有人知道如何将 sendBroadcast 和 BroadcastReceiver 用于不同的应用程序吗?实际上我已经在同一个项目中使用过 sendBroadcast 和 BroadcastReceiver。现在我想尝试发送到另一个应用程序。有人知道吗?
在我之前的项目中,我在 mainActivity 中这样广播:
Intent broadCastIntent = new Intent("SendMessage");
broadCastIntent.putExtra("NAME", gameName);
broadCastIntent.putExtra("JOB",jobStatus);
broadCastIntent.putExtra("STATUS",gameStatus);
sendBroadcast( broadCastIntent );
Log.d("Broadcast sent", gameName );
我还添加了检查意图的方法:
protected void onResume()
{
if (receiver == null)
{
receiver = new myBroadcastReceiver(); --> Here I call the receiver from another package
}
registerReceiver(reciever, new IntentFilter("SendMessage"));
}
@Override
protected void onPause()
{
super.onPause();
unregisterReceiver(reciever);
}
在另一个包中,但在一个项目中,我为接收意图创建了 myBroadcastReceiver 类:
public class myBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
String status = intent.getStringExtra("STATUS");
String job = intent.getStringExtra("JOB");
String media = intent.getStringExtra("MEDIA");
GameWorldExtension.job = job;
GameWorldExtension.media = media;
GameWorldExtension.status = status;
Log.d("receiver", "Got message: " + GameWorldExtension.status);
}
}
我试过了,效果很好。现在我想尝试发送到另一个应用程序。我尝试了很多方法,但都没有成功。有人知道如何按正确的顺序发送吗?谢谢