1

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.

user1726619
  • 941
  • 2
  • 18
  • 39
  • What version of Android are you targeting and testing this on? – Squonk Oct 25 '12 at 16:58
  • I am targeting target sdk version - 16,minsdkversion - 7 and testing on a device having api version 4.0.3 – user1726619 Oct 25 '12 at 17:01
  • Do you have any Activities in your application? Was the application ever started explicitly by the user? – David Wasser Oct 25 '12 at 17:04
  • 4
    @user1726619 : That might explain it. Later Android versions don't allow `BroadcastReceivers` to be automatically registered in order to prevent malware being silently installed. You will need to have an `Activity` that has to be manually started at least once in order for the `BroadcastReceivers` to be registered. – Squonk Oct 25 '12 at 17:04
  • Yes sir the application is having 10 activities and i have installed and already run it once before switching off the phone. – user1726619 Oct 25 '12 at 17:05
  • "Start an activity that has to be manually started" Can you please elaborate sir?I also request you to give few lines of code to remove my doubt. – user1726619 Oct 25 '12 at 17:08
  • 2
    @user1726619 : As David Wasser suggests, at least one application component (Example an `Activity`) must be started by the user after the app is installed. Until that is done, Android will not register any `BroadcastReceivers` declared in the manifest. If you *are* starting an `Activity` after installation then your code looks like it should work so I don't know what else to suggest. – Squonk Oct 25 '12 at 17:15
  • 4
    @user1726619 Have you requested the `RECEIVE_BOOT_COMPLETED` permission in your manifest? – acj Oct 25 '12 at 17:21
  • @acj This was the problem.Thanks a lot. – user1726619 Oct 25 '12 at 17:26

1 Answers1

2

Be sure to request the RECEIVE_BOOT_COMPLETED permission in your manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
acj
  • 4,821
  • 4
  • 34
  • 49