-3

I am trying to automatically start an Android service when the device reboots. I have tried to accomplish this using Receive_Boot_Complete permission, BroadcastReceiver with Boot_Complete intent action with no success. I'm very well aware that after Android 3.0 apps are placed in a stopped state on reboot and therefore no receivers are able to run. However, there are several mobile security apps such as Lookout that are run services and processes on reboot. How are they able to accomplish this?

<!-- Listen for the device starting up -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <receiver android:name="com.tpss.beacon.BootCompleteReceiver">
            <intent-filter>
                      <action android:name="android.intent.action.BOOT_COMPLETED"/>
                      <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
    </receiver>     


  public class BootCompleteReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

    context.startService(new Intent(context, UpdateGeofenceService.class));  
   }
}   
Kara
  • 6,115
  • 16
  • 50
  • 57
DollaBill
  • 239
  • 1
  • 3
  • 15

1 Answers1

0

When the system boots, it sends a broadcast of BOOT_COMPLETED intent. So, you have to create a BroadcastReceiver to catch that intent:

public class BReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, final Intent intent)
{
    Log.i(TAG, "onReveive BOOT_COMPLETED");

            // start your service
            context.startService(new Intent("your_service"));
    }
}

Then you modify the manifest:

<receiver android:name=".BReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

Don't forget to add the permission:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
HatemTmi
  • 1,068
  • 9
  • 16
  • Thanks for responding. Your suggestion is the approach I am currently using but haven't been able to get to work. – DollaBill May 28 '14 at 15:28