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

fumotvh3  于 2023-03-11  发布在  Android
关注(0)|答案(7)|浏览(190)

我有应用程序A和应用程序B。在应用程序A中,我想向应用程序B发送广播。这是应用程序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);
    }
}

我收到错误- * 接收者未注册 *。

gkl3eglg

gkl3eglg1#

首先,在manifest文件中声明应用B中的接收器,如下所示:

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

向Intent src(http://www.techotopia.com/index.php/Android_Broadcast_Intents_and_Broadcast_Receivers)发送广播add FLAG_INCLUDE_STOPPED_PACKAGES标志时,因为当您从应用A向应用B广播时,应用B可能未运行,此标志可确保即使应用未运行,广播也会到达:
FLAG_INCLUDE_STOPPED_PACKAGES标志在发送Intent之前添加到Intent,以指示允许Intent启动已停止应用程序的组件。

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

wyyhbhjk2#

在应用程序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>

在应用程序B主活动中:在create()时注册接收方,并在Destroy()时注销

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广播接收器中

public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String data = intent.getStringExtra("data");
        Log.i("BR" ,"Data received:  " + data);
    }
}
ajsxfq5m

ajsxfq5m3#

可能有两种情况:
1.您的appB没有运行,因此Activity没有示例化,因此接收器没有注册。
1.您的活动已被销毁,这意味着您已在onCreate()中注销了通过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中编写逻辑以显示相关数据/启动新活动

yws3nbqq

yws3nbqq4#

MyReceiver是类而不是对象。创建

myReceiver = new MyReceiver();

并放入...........

registerReceiver(myReceiver,intentFilter);
l0oc07j2

l0oc07j25#

如果这对某人有帮助,对我也有效
在活动中的应用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中

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

            }

            }
       }
zf2sa74q

zf2sa74q6#

我需要调用setPackage("package_name")以确保在Manifest中注册广播接收器时的明确性。然后,即使应用程序完全关闭,我也能够接收数据。

// 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>

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

n3ipq98p

n3ipq98p7#

使用这个,你可以向任何应用程序进行广播(我也为系统应用程序做了这个),如果你喜欢,你可以添加任何权限。
例如:将以下代码按钮放入应用程序A

Intent intentT = new Intent();
    intentT.setAction("com.android.example.A");
    intentT.putExtra("state", "1");
    sendBroadcast(intentT);

在应用B内创建类

public class test extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("com.android.example.A")) {
          //Do whatever you want}}}

在应用程序B * 安卓清单.xml* 内

<receiver android:name=".test"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.android.example.A" />
        </intent-filter>
    </receiver>

谢谢你。

相关问题