Search code examples
androidandroid-broadcastreceiver

BroadcastReceiver doesnt receive when app is closed


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>

Solution

  • 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
        }