Skip to content Skip to sidebar Skip to footer

How To Create Forground Service Never Stop When Application Is Inactive

I have created one application in which I want to start service First time open the application.when application is in foreground Every 5 or 10 minutes Local Notification received.

Solution 1:

You can use Alarm Manager to schedule a future task in specific time and set it again when it triggered. Sample usage or the new api WorkManager which could help to run task even app got killed.

But i think it is bad practice to keep a service running when your app is removed from system task as the service keep consuming memory & power. Better follow this Guideline

Solution 2:

publicclassMyServiceextendsService {
staticfinalintNOTIFICATION_ID=100;

publicstaticbooleanisServiceRunning=false;

@OverridepublicvoidonCreate() {
    super.onCreate();
    startServiceWithNotification();
}

@OverridepublicintonStartCommand(Intent intent, int flags, int startId) {
    if (intent != null && intent.getAction().equals(C.ACTION_START_SERVICE)) {
        startServiceWithNotification();
    }
    else stopMyService();
    return START_STICKY;
}

// In case the service is deleted or crashes some how@OverridepublicvoidonDestroy() {
    isServiceRunning = false;
    super.onDestroy();
}

@Overridepublic IBinder onBind(Intent intent) {
    // Used only in case of bound services.returnnull;
}


voidstartServiceWithNotification() {
    if (isServiceRunning) return;
    isServiceRunning = true;

    IntentnotificationIntent=newIntent(getApplicationContext(), MyActivity.class);
    notificationIntent.setAction(C.ACTION_MAIN);  // A string containing the action name
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntentcontentPendingIntent= PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Bitmapicon= BitmapFactory.decodeResource(getResources(), R.drawable.my_icon);

    Notificationnotification=newNotificationCompat.Builder(this)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setTicker(getResources().getString(R.string.app_name))
            .setContentText(getResources().getString(R.string.my_string))
            .setSmallIcon(R.drawable.my_icon)
            .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
            .setContentIntent(contentPendingIntent)
            .setOngoing(true)
//                .setDeleteIntent(contentPendingIntent)  // if needed
            .build();
    notification.flags = notification.flags | Notification.FLAG_NO_CLEAR;     // NO_CLEAR makes the notification stay when the user performs a "delete all" command
    startForeground(NOTIFICATION_ID, notification);
}

voidstopMyService() {
    stopForeground(true);
    stopSelf();
    isServiceRunning = false;
}
}

C = are the Strings that must start with the package name

Post a Comment for "How To Create Forground Service Never Stop When Application Is Inactive"