Reputation: 2266
I have two applications A1 and B1.A1 have a broadcastreceiver and i want to register this broadcastreceiver from B1.So I tried
Intent intent = new Intent();
intent.setClassName("pkgname","pkgname.BroadCastReceiverName");
intent.setAction("xxx.x...xxx");
getApplicationContext().sendBroadcast(intent);
But it does not fire/register any receiver.
How to access the broadcastreceiver of one application in another application?
Thanks in Advance
Upvotes: 2
Views: 3667
Reputation: 975
Hope this will work for you,
In App1: calling a Broadcast Receiver(Name of my broadcast receiver "MyBroadCastReceiver") of App2.
you can place this method in any Button onClick() or as per your requirement.
private void getAnotherAppMethod(){
Intent intent = new Intent("Updated");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
// for Example, here packageName of app2 is "com.app2.example" and its class name with packageName can be like "com.app2.example.yourBroadCastRecevier"
intent.setComponent(new ComponentName("package name of app2","package.yourbroadcastreciverName in app2"));
getContext().sendBroadcast(intent);
}
In App2: Called broadcast receiver which is Located in App2
public class MyBroadCastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent != null)
{
String sIntentAction = intent.getAction();
if (sIntentAction != null && sIntentAction.equals("youActionName"))
{
Toast.makeText(context, "Hello From App2", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(context,"Something went wrong",Toast.LENGTH_SHORT).show();
}
}
}
}
In App2: AndroidManifest.xml file add this below code inside your application tag
<receiver
android:name=".MyBroadCastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="youActionName" />
</intent-filter>
</receiver>
Upvotes: 1
Reputation: 14022
You can create a method(for example register()) in an Activity of App A1(for example Activity1) and add some code to that to registers it's brodcastreceiver.In onCreate() of Activity1 check extras of it's Intent
if it has a key for example reg
invoke register() in onCreate() of Activity1. Now, when you want to register your broadcastreciever,it is enough to start Activity1 from App B1 with an intent that has an extra with that specific key,for example reg
.
Upvotes: 0
Reputation: 5954
First of all always put some error checking when using reflection like this, because required package and receiver might not be available on users phone. The above method should work but you have to set some prerequisites before it can: Set android:exported="true" to your broadcast receiver in another application Run the other application at least once after install because global receivers like this get registered only if the application is ran at list once by the user. I think this is the case from android 4.x onwards but could be the same on older versions (if someone knows exact version when this was changed please add)
Upvotes: 0
Reputation: 327
The broadcastReceiver in your application-B1 should be registered in the AndroidManifest.xml file with the appropriate intent-filter that you are broadcasting from application-A1.
Upvotes: 0