I have App A and App B. In App A I want to send broadcast to App B. This is the code for App A:
final Intent intent = new Intent(); intent.setAction("com.pkg.perform.Ruby"); intent.putExtra("KeyName", "code1id"); intent.setComponent(new ComponentName("com.pkg.AppB", "com.pkg.AppB.MainActivity")); sendBroadcast(intent);
And in App B - In MainActivity
, I have MyBroadCastReceiver
Class.
public class MainActivity extends Activity { private MyBroadcastReceiver MyReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Receive broadcast from External App IntentFilter intentFilter = new IntentFilter("com.pkg.perform.Ruby"); MyReceiver = new MyBroadcastReceiver(); if(intentFilter != null) { registerReceiver(MyReceiver, intentFilter); } } public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(MainActivity.this, "Data Received from External App", Toast.LENGTH_SHORT).show(); } } @Override protected void onDestroy() { super.onDestroy(); if(MyReceiver != null) unregisterReceiver(MyReceiver); } }
I am getting the error - Receiver is not registered.
Open the Settings app. Scroll down, tap Utilities, and tap Parallel Apps. You'll see a list of apps that you can make copies of—not every app is supported. Find the app you want to clone, and turn its toggle to the On position.
The sendBroadcast(Intent) method sends broadcasts to all receivers in an undefined order. This is called a Normal Broadcast. This is more efficient, but means that receivers cannot read results from other receivers, propagate data received from the broadcast, or abort the broadcast.
To take the user from one activity to another, your app must use an Intent to define your app's "intent" to do something. When you pass an Intent to the system with a method such as startActivity() , the system uses the Intent to identify and start the appropriate app component.
There are a couple of different methods you can use to easily send an app or game directly to other Android users. “ Nearby Share ” is an Android feature similar to AirDrop on the iPhone, iPad, and Mac. You don’t need to exchange contact info or be on the same network as someone to send them something. They just need to be physically nearby.
This method is nice because it sends the actual file through the app store, which means that the recipient can quickly install it without manually downloading it first. First, open the Play Store app on your Android phone or tablet. Tap the hamburger menu in the top-left to open the overflow menu. Next, select “My Apps & Games” from the menu.
The Google Play Store has Nearby Share functionality built in for sending apps and games to people. This method is nice because it sends the actual file through the app store, which means that the recipient can quickly install it without manually downloading it first. First, open the Play Store app on your Android phone or tablet.
Again, we start from the Google Play Store. Find the app or game that you want to share and tap the three-dot menu icon in the top-right corner. Next, select “Share” from the menu. Android’s native share menu will open.
First thing first declare the receiver in app B in the manifest file like this:
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.pkg.perform.Ruby" /> </intent-filter> </receiver>
when sending the broadcast add FLAG_INCLUDE_STOPPED_PACKAGES
flag to the intent [src] because when you broadcast from app A to app B , app B might not be running, this flag insures that the broadcast reachs out even apps not running:
FLAG_INCLUDE_STOPPED_PACKAGES flag is added to the intent before it is sent to indicate that the intent is to be allowed to start a component of a stopped application.
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
In your case it will be like this:
final Intent intent=new Intent(); intent.setAction("com.pkg.perform.Ruby"); intent.putExtra("KeyName","code1id"); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.setComponent( new ComponentName("com.pkg.AppB","com.pkg.AppB.MyBroadcastReceiver")); sendBroadcast(intent);
In App A: Send the broadcast here.
final Intent i= new Intent(); i.putExtra("data", "Some data"); i.setAction("com.pkg.perform.Ruby"); i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); getApplicationContext().sendBroadcast(i);
In App B manifest
<receiver android:name=".MyBroadcastReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.pkg.perform.Ruby" /> </intent-filter> </receiver>
In App B MainActivity: register the receiver oncreate(), and unregister onDestroy()
public class MainActivity extends AppCompatActivity { private MyBroadcastReceiver MyReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyReceiver = new MyBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter("com.pkg.perform.Ruby"); if(intentFilter != null) { registerReceiver(MyReceiver, intentFilter); } } @Override protected void onDestroy() { super.onDestroy(); if(MyReceiver != null) unregisterReceiver(MyReceiver); } }
In App B BroadcastReceiver
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String data = intent.getStringExtra("data"); Log.i("BR" ,"Data received: " + data); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With