BroadcastReceiver doesnt receive when app is close

2019-08-27 23:29发布

问题:

I know this question is similar to many questions of BroadcastReceiver but as I read, non of them have solutions. the tutorial of BroadcastReceiver tells it will work even app was not running in the background, my question is why I can not use it when app is not running I tried to call broadcast from main activity, use service and .... but non of them solved my problem.

here is my CODE:

MyReceiver java Class:

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"BroadCast Trigger",Toast.LENGTH_SHORT).show();
    }
 }

Also MyManifest Code:

<receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.media.VOLUME_CHANGED_ACTION" />
    </intent-filter>
</receiver>

回答1:

As I found there is no way to active BroadcastReceiver in Huawei Devices programmatically, but here is a solution to find device type and do needed action in this regard such as show an alert to user to activate it manually.

        if ("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
        // Do Needed Action
    }


回答2:

I faced with same problem on Huawey Honor with Android 7. On Sony and ZTE devices BroadcastReceiver works as expected. But on Honor it work some time and suddenly stop. I discover, that problem not related with re-boot. I reboot device and broadcast receiver work after it. But sometimes, it stop without rebooting.

First i add my app to protected list according this solution: "Protected Apps" setting on Huawei phones, and how to handle it

But it didn't help :(

Then, i add a fake accessibility service to my app, according to this recommendation: Broadcast Receiver Not Working After Device Reboot in Android

And problem was solved!