Android broadcastreceiver not triggering when app is not running

前提是你 提交于 2019-12-02 06:31:23

Try to set android:enabled="true" to your receiver

Like this

 <receiver android:name=".Receiver"
    android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
        </intent-filter>
    </receiver>

Update

I just implemented it in my app your's way and it worked. But I did it with right click->new->other->broadcast receiver

After inserting intent-filter I got code like this

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

And it actually calling onReceive method

Did you try using wake-locks? Generally app will wake up on the receive of broascast butif it doesnot (happens in few devices), then try using wake-locks which will forcefully wake up the device.

How to use wake-locks :

//Register for wake-locks

if (mWakeLock == null) {
        PowerManager pm = (PowerManager)yourcontext.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, yourcontext);
        mWakeLock.setReferenceCounted(false);

}

//Apply wake-lock using acquire

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