Reputation: 83
Anybody knows how to use sendBroadcast and BroadcastReceiver for different application? Actually I have already used sendBroadcast and BroadcastReceiver but in the same project. Now I want to try to send to another application. Anybody knows?
In my previous project I broadcast like this in 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 );
Also I add method to check the intent:
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);
}
And in another package but in one project , I have created myBroadcastReceiver class for Receive the intent:
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);
}
}
I have try and it works fine. Right now I want to try to send into another application. I have tried many ways, but it didn't successful. Anybody knows how to send in right order?Thanks
Upvotes: 1
Views: 144
Reputation: 44
I think this post could help you. There is an example of BroadcastReceiver that listening "onWifiChange" event. How to use Broadcast Receiver in different Applications in Android?
------- added
On Sender side:
1) Sender class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent("pacman.intent.action.BROADCAST");
intent.putExtra("message","Wake up.");
sendBroadcast(intent);
}
On Receiver side:
1) Receiver class:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Message at Pacman received!");
}
}
2) Receiver manifest file:
<receiver android:name="com.ex.myapplication2.MyBroadcastReceiver">
<intent-filter>
<action android:name="pacman.intent.action.BROADCAST" />
</intent-filter>
</receiver>
Upvotes: 3