Android Foreground Service Slow If Device Is Idle
Solution 1:
I have made this library the other day for something similar service in background and foreground
And its working absolutely fine with no problems.
The reason I choose to run it with AlarmManager
is that the AlarmManager
is a great candidate for scheduling if an application needs to perform a local event + allows an application to schedule tasks that may need to run or repeat beyond the scope of its lifecycle. This allows the application to perform some function even after the application process or all of its Android components have been cleaned up by the system.
UPDATE
Call this method to start the service
publicvoidcall(int Value_in_seconds) {
if (Value_in_seconds == (int) Value_in_seconds) {
// Number is integer
Long time = new GregorianCalendar().getTimeInMillis() + Value_in_seconds * 1000;
// create an Intent and set the class which will execute when Alarm triggers, here// ServiceReciever in the Intent, the onRecieve() method of this class will execute when// alarm triggers
Intent intentAlarm = new Intent(context, ServiceReciever.class);
// create the object
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(context, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
} else {
Toast.makeText(context, context.getString(R.string.intValue), Toast.LENGTH_SHORT).show();
}
}
Create ServiceReciever
Class
publicclassServiceRecieverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent intent) {
//call the method here
}
}
inside your manifest
<application><receiverandroid:name="hossamscott.com.github.backgroundservice.ServiceReciever"android:process=":ff"android:exported="true"android:enabled="true"></receiver><serviceandroid:name="hossamscott.com.github.backgroundservice.BackgroundTask"/></application>
And thats should be it, tho if you like to run it in Thread
Than you can add the next lines
publicclassBackgroundTaskextendsService {
privateboolean isRunning;
privateContext context;
privateThread backgroundThread;
@Overridepublic IBinder onBind(Intent intent) {
returnnull;
}
@OverridepublicvoidonCreate() {
this.context = this;
this.isRunning = false;
this.backgroundThread = newThread(myTask);
}
privateRunnable myTask = newRunnable() {
publicvoidrun() {
// Do something herenewHandler(Looper.getMainLooper()).post(newRunnable() {
@Overridepublicvoidrun() {
// do your logic here
}
});
stopSelf();
}
};
@OverridepublicvoidonDestroy() {
this.isRunning = false;
}
@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {
if (!this.isRunning) {
this.isRunning = true;
this.backgroundThread.start();
}
returnSTART_STICKY;
}
}
And to call this class
edit ServiceReciever
to be like this
@OverridepublicvoidonReceive(Context context, Intent intent) {
//call the method hereIntentbackground=newIntent(context, BackgroundTask.class);
context.startService(background);
}
Post a Comment for "Android Foreground Service Slow If Device Is Idle"