广播接收器不工作 - 为什么会这样?

0 static android broadcastreceiver

我正在学习安卓。我尝试实现自定义静态广播接收器,但它不起作用。我从谷歌搜索一些问题,但找不到解决这个问题的方法。我使用 Android 7.0 最低级别 24 目标级别 28

事实上,当我启动活动时,MyStaticReceiver 没有启动(无日志) MyDynamicReceiver 工作完美

你有解决方案吗?

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test.receiver">

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

        <activity android:name="test.receiver.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyStaticReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="@string/StaticAction" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java:

public class MainActivity extends Activity {
    public final static boolean Debug = true;
    public final static String TAG = "TagDebug";

    private MyDynamicReceiver dynamicReceiver;

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

        ComponentName receiver = new ComponentName(this, MyStaticReceiver.class);
        PackageManager pm = this.getPackageManager();
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Debug) Log.i(TAG, "MainActivity:onResume");
        configureDynamicReceiver();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if (Debug) Log.i(TAG, "MainActivity:onDestroy");
        unregisterReceiver(dynamicReceiver);
    }

    public void onClickButton(View v) {
        if (Debug) Log.i(TAG, "MainActivity:onClickButton");
        Intent staticIntent = new Intent();
        staticIntent.setAction(getString(R.string.StaticAction));
        sendBroadcast(staticIntent);

        Intent dynamicIntent = new Intent();
        dynamicIntent.setAction(getString(R.string.DynamicAction));
        sendBroadcast(dynamicIntent);
    }

    public void configureDynamicReceiver() {
        if( dynamicReceiver == null ) {
            dynamicReceiver = new MyDynamicReceiver();
        }
        IntentFilter filter = new IntentFilter(getString(R.string.DynamicAction));
        registerReceiver(dynamicReceiver, filter);
    }
}
Run Code Online (Sandbox Code Playgroud)

MyStaticReceiver.java(MyDynamicReceiver 是相同的...)

public class MyStaticReceiver extends BroadcastReceiver {
    public final static boolean Debug = true;
    public final static String TAG = "TagDebug";

    public MyStaticReceiver() {
        if (Debug) Log.i(TAG, "MyStaticReceiver");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Debug) Log.i(TAG, "MyStaticReceiver:onReceive");
    }
}
Run Code Online (Sandbox Code Playgroud)

G B*_*G B 11

您实际上正在发送隐式广播,因此清单中声明的​​接收器将不起作用。

如果您的应用面向 Android 8.0(API 级别 26)或更高版本,则无法使用清单来声明大多数隐式广播(不专门针对您的应用的广播)的接收器。当用户主动使用您的应用程序时,您仍然可以使用上下文注册的接收器。关联

您不会遇到任何问题,MyDynamicReceiver因为它是上下文注册的接收器。

但为了使其工作MyStaticReceiver,您可以尝试通过在 的构造函数中传递组件名称来发送显式广播Intent

Intent staticIntent = new Intent(this, MyStaticReceiver.class);
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);
Run Code Online (Sandbox Code Playgroud)