How to Send BroadCast from one app to another app

2019-01-07 13:34发布

问题:

I have App A and App B. In App A i want to send broadcast to App B. This is the code for App1:

 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 My BroadCastReceiverClass

     public class MainActivity extends Activity {
        private MyBroadcastReceiver MyReceiver;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                // Receive broad Cast fromn External App
                IntentFilter intentFilter=new 
               MyReceiver =new MyBroadcastReceiver();
                IntentFilter("com.pkg.perform.Ruby");
                if(intentFilter!=null)
                {
                    registerReceiver(MyReceiver,intentFilter);

                }
        }

   public class MyBroadcastReceiver extends BroadcastReceiver
    {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub


            Toast.makeText(MainActivity.this,"Data Received from External App",Toast.LENGTH_SHORT).show();

        }

    }



    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(MyReceiver!= null)
        unregisterReceiver(MyReceiver);;
    }
    }

I am getting the error Receiver is not registered .

回答1:

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.MainActivity"));  
    sendBroadcast(intent);


回答2:

There may be two cases :

  1. Your appB is not running, hence the activity is not instantiated, and so the receiver is not registered.
  2. Your activity is destroyed, means that you have unregistered your receiver that you registered via registerReceiver() in onCreate()

Solution :

Register your broadcast receiver in manifest.

Inside manifest of appB :

<receiver android:name=".MyBroadcastReceiver">
    <intent-filter>
        <action android:name="com.pkg.perform.Ruby" />
    </intent-filter>
</receiver>

And comment out the line in appA

intent.setComponent(new ComponentName("com.pkg.AppB","com.pkg.AppB.MainActivity"));

Write the logic in MyBroadcastReceiver to display relevant data/launch new activity



回答3:

MyReceiver is class not object. Create

myReceiver = new MyReceiver(); 

and put...........

registerReceiver(myReceiver,intentFilter);


回答4:

If this helps some one and it works for me

In App A in activity or in a content provider-

  Intent intent = new Intent("Updated");
  intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
  intent.setComponent (new
  ComponentName "com.exam.appA",
  "com.exam.appA.DbaseChanged"));
  getContext().sendBroadcast(intent);

In App B in the manifest

      <receiver
        android:name=".DbaseChanged"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="Updated" />
        </intent-filter>
      </receiver>

In App B Broadcast receiver class-

    public class DbaseChanged extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent
     intent)   {

           String act = intent.getAction();
           if(act != null && act.equals("Updated")  )
            {
                Toast.makeText(context, act ,
              Toast.LENGTH_SHORT).show();

            }

            }
       }