broadcastreceiver in case of android device switched off and on not working

99封情书 提交于 2019-12-11 09:43:36

问题


I am using broadcast receivers for performing some action when the device is switched off and switched on again,but they are not working.These are the receivers in manifest file:

  <receiver android:name=".ShutdownReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
            </intent-filter>
        </receiver>
        <receiver android:name=".RestartReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver> 

These are the corresponding classes:

public class ShutdownReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Log.d("In","Switched Off");
    }
}

public class RestartReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        SecureMessagesActivity.ToDoOnMobileSwitchOn();
        Log.d("In","Switched On");
    }
}

Please help me.Thanks in advance.


回答1:


Be sure to request the RECEIVE_BOOT_COMPLETED permission in your manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>


来源:https://stackoverflow.com/questions/13073461/broadcastreceiver-in-case-of-android-device-switched-off-and-on-not-working

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