Unfortunately, there is currently no reliable way to receive a broadcast event after installing your application, ACTION_PACKAGE_ADDED . Intent does not translate to a newly installed package.
To receive the ACTION_BOOT_COMPLETED event , you need the broadcast receiver class as well as your service. I would also recommend adding ACTION_USER_PRESENT the intention to be caught by this broadcast receiver, this requires Android 1.5 (minSDK = 3), this will call your broadcast receiver when the user unlocks his phone. The last thing you can do to try and save your work without turning it off automatically is to call Service.setForeground () on your onCreate service to tell Android that your service should not stop, this was added mainly for services like mp3 - players that must continue to work, but can be used by any service.
Make sure you add the correct permissions for the boot_complete and user_present events in the manifest.
Here is a simple class that you can use as a broadcast receiver for events.
package com.snctln.util.WeatherStatus; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class WeatherStatusServiceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction() != null) { if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED) || intent.getAction().equals(Intent.ACTION_USER_PRESENT)) { context.startService(new Intent(context, WeatherStatusService.class)); } } } };
Good luck.
snctln Jun 13 '09 at 16:10 2009-06-13 16:10
source share