如何将BroadCast从一个应用程序发送到另一个应用程序

时间:2015-11-03 06:34:41

标签: android broadcastreceiver broadcast

我有App A和App B.在App A中我想向App B发送广播。 这是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);

在应用B中 - 在MainActivity中,我有MyBroadCastReceiver类。

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);
    }
}

我收到错误 - Receiver未注册

7 个答案:

答案 0 :(得分:41)

首先,首先在清单文件中的app B中声明接收器,如下所示:

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

向广告 [src]发送广播添加FLAG_INCLUDE_STOPPED_PACKAGES标志时,因为当您从应用A广播到应用B时,应用B可能没有正在运行,此标志可确保广播覆盖甚至应用没跑:

  

FLAG_INCLUDE_STOPPED_PACKAGES标志被添加到之前的意图中   被发送以表明允许意图开始   已停止的应用程序的组件。

intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);

在你的情况下,它将是这样的:

    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);

答案 1 :(得分:2)

可能有两种情况:

  
      
  1. 您的appB未运行,因此活动未实例化,因此接收方未注册。
  2.   
  3. 您的活动已被销毁,意味着您已取消注册您通过onCreate()
  4. 中的registerReceiver()注册的接收者   

解决方案:

  

在清单中注册您的广播接收器。

appB的内部清单:

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

并在appA中注释掉该行

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

在MyBroadcastReceiver中编写逻辑以显示相关数据/启动新活动

答案 2 :(得分:0)

MyReceiver是类而不是对象。创建

myReceiver = new MyReceiver(); 

并放...........

registerReceiver(myReceiver,intentFilter);

答案 3 :(得分:0)

如果这对某人有帮助,并且对我有用

在活动中的App A中或在内容提供商中-

  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);

在清单B中的应用B中

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

在应用B中,广播接收器类别-

    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();

            }

            }
       }

答案 4 :(得分:0)

在应用A中:在此处发送广播。

 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);

在应用B清单中

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

在App B MainActivity中:注册接收者oncreate(),然后取消注册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);
}}

在应用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);
    }
}

答案 5 :(得分:0)

在Android 9(API级别28)中,我只需要在代码中定义BroadcastReceiver即可。

就像这样(在我的情况下,添加到服务中-没关系)

private MyReceiver myReceiver;

@Override
public void onCreate() {
    myReceiver = new MyReceiver();
    this.registerReceiver(myReceiver, new IntentFilter("android.intent.action.myreceiver"));
}


@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(myReceiver);
}

private class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {

        try {
            if (intent.getAction().equals("android.intent.action.myreceiver")) {
                //logic goes here
            }
        } catch (Exception e) {
            //some log goes here
        }
    }
}

像这样发送广播

Intent intentMy = new Intent();
intentMy.setAction("android.intent.action.myreceiver");
intentMy.putExtra("whatever", true);
sendBroadcast(intentMy);

答案 6 :(得分:0)

在清单中注册广播接收器时,我需要致电setPackage("package_name")以确保明确。这样,即使应用程序完全关闭,我也可以接收数据。

// sending app sends broadcast
Intent intent = new Intent(ACTION_RECOMMEND);
intent.putExtra(LISTEN_RECOMMENDATION, "Triggered - Jhene Aiko");
intent.putExtra(WATCH_RECOMMENDATION, "Goblin - Kim Go-eun");
intent.setPackage("com.example.package.receiverapp");
sendBroadcast(intent);


//receiving app manifest registers receiver
<receiver
   android:name=".ManifestRegisteredBroadcastReceiver"
   android:exported="true">
   <intent-filter>
       <action android:name="com.random.action.RECOMMEND" />
   </intent-filter>
</receiver>

通过活动注册接收方时,我不需要添加intent.setPackage(package_name),但这也意味着如果活动被破坏(长时间关闭应用程序,后台运行应用程序),我将无法获取数据)