Broadcast receiver is not working - why so?

时光毁灭记忆、已成空白 提交于 2019-12-23 05:26:06

问题


I'm studying Android. I try to implement a Custom static Broadcast Receiver but it is not working. I search for some issue from Google but I can't find something to solve this. I work on Android 7.0 Min Level 24 Target Level 28

In fact, MyStaticReceiver isn't launching when I start the Activity (no log) MyDynamicReceiver work perfectly

Do you have a solution?

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 is the same ...)

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

回答1:


You are actually sending an implicit broadcast, therefore the receiver declared in the manifest will not work.

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

You don't face any issue with MyDynamicReceiver because it is context-registered receiver.

But to make it work for MyStaticReceiver, you can try sending an explicit broadcast by passing the component name in the constructor of the Intent.

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


来源:https://stackoverflow.com/questions/55610977/broadcast-receiver-is-not-working-why-so

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!