使用BroadcastReceiver进行信息传递(软件内部和软件之间都行,但是效率低):
1.创建一个类并继承于BroadcastReceiver类:class MyBC extends BroadcastReceiver
2.在AndroidManifest.xml文件的Application中添加一个receiver(MyBc)
3.发送广播信息(发送的是一个Intent):
Intent i = new Intent(MainActivity.this, MyBC.class); i.putExtra(“txt”, “Hello eoe”);
sendBroadcast(i);
4.接收广播信息(MyBc类中的onReceive()方法在有广播信息时会自动执行):
public void onReceive(Context context, Intent intent) {
Intent.getStringExtra(“txt”);
}
5.动态的注册方法:
final MyBC mybc = new MyBC();
registerReceiver(mybc, new IntentFilter(“cn.eoe.usingbc.intent.action.MyBC”));
6.注销:
unregisterReceiver(mybc);