Search code examples
androidbroadcastreceiveralarmmanagerintentfilter

Start app on device boot, but not run service


I have a BroadcastReceiver that is using the BOOT_COMPLETED intent-filter in the AndroidManifest, which then runs my Service when the device boots. Is it possible to have my Service start, but not actually run the code in the Service? I have an AlarmManager that run the Service at a regular interval, but, ideally, I'd like that code to not run when the device starts.

<receiver android:name="com.app.AlarmReceiver"> 
    <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter> 
</receiver>

Solution

  • Yes, call startActivity:

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent();
        i.setClassName("com.test", "com.test.MainActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }