BroadcastReceiver广播
广播类型:普通、有序 简单的举个例子,大体用法思路已经很清楚了,剩下的有时间在附上
实现:注册--->触发--->响应
public class MainActivity extends Activity{
private final String ACTION_NAME = "com.mengle.20150521";
private Button execute;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
execute = (Button) findViewById(R.id.execute);
// 注册广播
registerBoradcastReceiver();
execute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ACTION_NAME);
intent.putExtra("mengle", "要发送的内容>>我来了...");
// 发送广播
sendBroadcast(intent);
}
});
}
public void registerBoradcastReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_NAME);
// 注册广播
registerReceiver(mBroadcastReceiver, intentFilter);
}
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent
intent) {
String action = intent.getAction();
String str = intent.getStringExtra("mengle");
if (action.equals(ACTION_NAME)) {
Toast.makeText(MainActivity.this, str, 1000).show();
}
}
};
}