1

我有 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>

BroadcastReceiverIncoming 在 App1 中定义。

现在在 App2 中,我调用它来调用广播接收器,但它现在以某种方式工作?

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

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

如果我调用 BroadcastReceiverIncoming,则通过 adbadb shell am broadcast -n packageName/packagename.BroadcastReceiverIncoming 在 App1 中正确调用。

4

1 回答 1

2

我的工作如下:

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

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

于 2012-12-10T14:52:58.337 回答