【问题标题】:Broadcast from app1 to app2从 app1 广播到 app2
【发布时间】:2017-12-23 20:43:17
【问题描述】:

我正在尝试从 bc_from 广播到 bc_to。
如果我在 Activity bc_to 中使用它可以正常工作:

registerReceiver(receiver, filter);

如果我在 Manifest 中定义接收方,它不起作用。

我从文档中了解到,自 26 年以来这可能是不可能的。
因此,我正在寻找任何能够达到 Activity bc_to 的解决方案,即使它没有运行。

谢谢

// package com.yotam17.ori.bc_from;
public class MainActivity extends AppCompatActivity {

    private static final String BC_ACTION = "com.yotam17.ori.bc.Broadcast";

    private void send() {
        Intent intent = new Intent(BC_ACTION);
        intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
        sendBroadcast(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        send();
    }
}

bc_from 的代码包含 Manifest 和两个 clases:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <receiver android:name="com.yotam17.ori.bc_to.MyReceiver" >
        <intent-filter>
            <action android:name="com.yotam17.ori.bc.Broadcast"/>
        </intent-filter>
    </receiver>

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


// package com.yotam17.ori.bc_to;
public class MainActivity extends AppCompatActivity {

    private static final String BC_ACTION = "com.yotam17.ori.bc.Broadcast";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Register MyReceiver
        IntentFilter filter = new IntentFilter(BC_ACTION);
        MyReceiver receiver = new MyReceiver();
        registerReceiver(receiver, filter); //<<<<<< Does not work w/o this
    }
}


public class MyReceiver extends BroadcastReceiver {

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

        Toast.makeText(context, "Got it!!!" , Toast.LENGTH_SHORT).show();
    }
}

【问题讨论】:

  • 使用显式的Intent(例如,通过setComponent()),而不是隐式的Intent

标签: android android-pendingintent broadcast


【解决方案1】:

谢谢!
setComponent() 解决了它。
我之前从未使用过 setComponent(),但找到了一个详细的示例here


为了使它成为一个工作示例 - 这是修改后的 send() 代码:

private void send() {
    Intent intent = new Intent(BC_ACTION);
    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    intent.setComponent(new ComponentName("com.yotam17.ori.bc_to","com.yotam17.ori.bc_to.MyReceiver"));
    sendBroadcast(intent);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 2011-06-05
    • 2015-05-13
    • 1970-01-01
    • 2014-08-07
    相关资源
    最近更新 更多