BroadcastReceiverIncoming

BroadcastReceiverIncoming

我有2个应用,App1和App2

在App1中,清单文件具有定义如下的BroadcastReceiverIncoming。

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


在App1中定义了BroadcastReceiverIncoming。

现在在App2中,我将其称为调用广播接收器,但它现在正在以某种方式工作?

Intent intent = new Intent();
intent.setClassName("packageName", "packagename.BroadcastReceiverIncoming");
intent.setAction("packagename.BroadcastReceiverIncoming");
appContext.sendBroadcast(intent);


即使第2行已注释/未注释,它也不会调用BroadcastReceiverIncoming。

通过adb如果我呼叫adb shell am broadcast -n packageName/packagename.BroadcastReceiverIncoming
在App1中正确调用了BroadcastReceiverIncoming。

最佳答案

我的工作如下:

    myServiceReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context ctx, Intent data) {
            if (data.getAction().equals(POLayer.SVC_INTENT)) {
                final String alias = data.getStringExtra("alias");
                final String command = data.getStringExtra("action");
                if (command.equals("dosomething")) {
                    new DoSomethingTask().execute( alias );
                }
                             }
                     }
             };

  ....

  onCreate:
    final IntentFilter filter = new IntentFilter(POLayer.SVC_INTENT);
    filter.addAction(POLayer.ANOTHER_INTENT);
    this.registerReceiver(this.myServiceReceiver, filter);


在发件人中:

                Intent i = new Intent();
                i.setAction( SVC_INTENT );
                i.putExtra( "alias", theAlias );
                i.putExtra( "action", "dosomething" );
                this.sendBroadcast( i );


我看到的唯一区别是,我不依赖包/组件名称,而是定义自己的ACTION。另外,所有名称都区分大小写,因此您可能要检查packageName与packagename。

07-28 12:42