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

Broadcast receiver is not working - why so?

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

事实上,当我启动 Activity 时,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>

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

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

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

If your app targets Android 8.0 (API level 26) or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don't target your app specifically). You can still use a context-registered receiver when the user is actively using your app. Link

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

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

Intent staticIntent = new Intent(this, MyStaticReceiver.class);
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);