Step 1: Create MyReciever Class:
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)){ Intent serviceIntent = new Intent(context, MyService.class); context.startService(serviceIntent); } Intent serviceIntent = new Intent(context, MyService.class); context.startService(serviceIntent); } }
Step 2: Create MyService Class
import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.preference.PreferenceManager; public class MyService extends Service { public MyService() { } @Override public void onCreate() {
Step 3: In the launch class of the MainActivity Service:
startService(new Intent(this, MyService.class));
azatserzhan
source share