broadcastreceiver in case of android device switch

2019-08-04 09:49发布

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条回答
放我归山
2楼-- · 2019-08-04 10:13

Be sure to request the RECEIVE_BOOT_COMPLETED permission in your manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
查看更多
登录 后发表回答