I want to fetch some data from the background in fixed interval of time say 30 minutes. I have implemented the solution using a alarm manager where I call a Service in fixed interval. The process is working fine but the problem I am facing is that it is taking quite few battery power. I want to utilize the battery consumption so that the users don't run away from the application. Alarm is set up like the first part of the code.
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, PollingClass.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000, 30 * 60 * 1000, pi);
In the second part Service class is called by the alarm to execute the task.
public class PollingClass extends Service {
private WakeLock mWakeLock;
public PollingClass() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void handleIntent(Intent intent) {
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NEW");
if ((mWakeLock != null) && (mWakeLock.isHeld() == false)) {
mWakeLock.acquire();
}
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
if (!cm.getBackgroundDataSetting()) {
stopSelf();
return;
}
//Calling an Async class to fetch the data from the server
stopSelf();
}
#Override
public void onStart(Intent intent, int startId) {
handleIntent(intent);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleIntent(intent);
return START_NOT_STICKY;
}
public void onDestroy() {
super.onDestroy();
mWakeLock.release();
}
}
Thanks in advance.
IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work. IntentService has a background thread, but only to invoke onHandleIntent(). Once onHandleIntent() returns, not only does the thread go away, but the service gets destroyed. so this will help your code to run in different thread when you implement your code in onHandleIntent().
for example:
#Override
protected void onHandleIntent(Intent intent) {
//This is where you will put your volley code or some code that you want to perform in background
}
ThankYou. I hope this was helpful.
Related
Let me come straight to my issue.
There is an old similar question:
Continue Service even if application is cleared from Recent app. Please go through the link before going ahead.
I have to save some data to the local database before the application is destroyed completely.
I have following issues with onTaskRemoved() method of the service:
onTaskRemoved() method is not triggered every time the application(and hence the service) is stopped from the recent applications screen.
Even if onTaskRemoved() is called every time, the method is not executed completely. I mean if there are 20 statements in the method, only 5 or 10 statements are executed and the method breaks after that. Like if I have 20 System.out.println() statements than only 5 or 10 or 12(any random number) statement prints and method breaks after that.
So I can rely on the method onTaskRemoved() method for cleaning up resources acquired by the application.
This is a blocking issue for my application release. I have tried every trick. Like having at least one activity in the stack(I am calling it GhostActivity) so that if the application is killed from the recent applications screen, we can do clean up in onDestroyed() method the activity. onDestroyed() is called but it has exact same issues like onTaskRemoved() method.
I am stuck on the issue since weeks and it's very annoying. Please let me know if anyone has any solution.
I finally myself got it worked. I used a combination of service and alarm manager. Every-time I want to handle the force close scenario, I start a service in which I run an infinite loop. This loop will iterator every 15 secs. In the loop, I set an alarm 20 secs from the current time. Now if the next iteration of the loop happens the alarm is updated and reset to 20 secs after new current time. In this way, the alarm will trigger only if the service was not destroyed by the user by calling stopService() method.
public class MyIntentService extends IntentService {
public MyIntentService() {
super("My IntentService");
}
private boolean stopped = false;
private Thread runningThread;
private static MyIntentService mInstance;
#Override
public void onCreate() {
super.onCreate();
mInstance = MyIntentService.this;
}
#Override
public int onStartCommand(#Nullable Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
#Override
protected void onHandleIntent(Intent intent) {
stopped = false;
runningThread = Thread.currentThread();
while(!this.stopped) {
Intent intent = new Intent("Your_Custom_Broadcast_Action");
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent broadcastIntent = PendingIntent.getBroadcast(WifiService.this, CLEAN_UP_ALARM_REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 20000, broadcastIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 20000, broadcastIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 20000, broadcastIntent);
}
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public void onDestroy() {
super.onDestroy();
mInstance = null;
if (runningThread != null) {
runningThread.interrupt();
}
}
public void stopService() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
PendingIntent broadcastIntent = PendingIntent.getBroadcast(WifiService.this, CLEAN_UP_ALARM_REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(broadcastIntent);
stopped = true;
if (runningThread != null)
runningThread.interrupt();
}
public static MyIntentService getActiveInstance() {
return mInstance;
}
}
I have following design in my app: I have a one activity that sets repeating alarm that launches receiver that starts my service. Every minute. In my service, I set Start Sticky but once Android decides to kill my service, I can't get it to restart. It's Android 4.4.2. Here is my code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i1 = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, i1, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, 0, 60 * 1000, pi);
}
here is the receiver
public void onReceive(Context arg0, Intent arg1) {
// For our recurring task, we'll just display a message
Log.d(Constants.TAG, "Starting Service");
Intent intent = new Intent(arg0, MyLocationService.class);
arg0.startService(intent);
}
and service:
private static PowerManager.WakeLock wl;
private static OkHttpClient client;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(Constants.TAG, "Service onStartCommand");
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, Constants.TAG);
wl.acquire();
client = new OkHttpClient();
new GetLocation(MyLocationService.this).execute();
return START_STICKY;
}
You're most likely seeing an interaction with power management and alarms. Starting with API 19 all alarms are inexact by default, so they will be collated with other alarms. Further, once the device has entered into a lower power state, alarms are delivered to BroadcastReceivers and the device guaranteed to stay awake as long as the receiver is executing its onReceive() method. Once it returns from that method (and any other BroadcastReceiver associated with the alarm runs), the device will immediately go to a low power state again. Since your app had previously been killed, the Service is no longer running and will not get CPU time.
The way to resolve this is to use a WakefulReceiver which takes a wakelock when it runs onReceive(), starts your Service for processing. The Service will then release the wakelock when it is done processing. This article will give you a good explanation of this: http://po.st/7UpipA
Note that waking up every minute is going to seriously degrade the battery life of the device, so you should consider backing this off.
If I start a service with startService in a Activity I get:
1 processes and 1 service
If I now swipe that Activity away. I.e remove it, I get:
0 processes and 1 service
Why is this? And what is a Process and what is a Service in the Android world?
I use START_STICKY and if I stop the service via Settings, Apps and Running, it is not started again, why?
Update1 some code:
Activity:
startService(new Intent(getApplicationContext(), MyService.class));
Service:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Starting service");
return(START_STICKY);
}
what is the definition of process in android world? same as defined at any operating system - your application is "alive" from the system's point of view, it has active memory allocation stack, and may run or not Activities, Services and so on...
I think that you struggling your had "how can it be that running process = 0" but services = 1 not making scenes, and you are right.
the running applications display shown from the settings app is not made only for developers, but also for users, I guess that's why most vendors decided to show active tasks as process. basically, in this display - running process = running task.
most application starts only one task (the main activity with the launcher flag starts automatically in that mode). there will be more tasks only if other activities would start explicitly with that flag.
so, if your app have 2 activities that started at new task mode - you'll see "2 process".
if your app not running at all (your process really not alive) - then you won't see the app in the running apps screen.
Turned out to be a bug in KitKat.
(Sometimes I think getting anything done in Android is a big hassle!)
Android Services: START_STICKY does not work on Kitkat
https://code.google.com/p/android/issues/detail?id=63793
Fix in Service:
#Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(getApplicationContext(), this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
}
The Main problem in your case is ur unable to start the service when app closed,that time android OS will kill the service, If you are not able to restart the service then call a alam manger to start the reciver like this,
Manifest is,
<service
android:name=".BackgroundService"
android:description="#string/app_name"
android:enabled="true"
android:label="Notification" />
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="REFRESH_THIS" />
</intent-filter>
</receiver>
IN Main Activty start alarm manger in this way,
String alarm = Context.ALARM_SERVICE;
AlarmManager am = (AlarmManager) getSystemService(alarm);
Intent intent = new Intent("REFRESH_THIS");
PendingIntent pi = PendingIntent.getBroadcast(this, 123456789, intent, 0);
int type = AlarmManager.RTC_WAKEUP;
long interval = 1000 * 50;
am.setInexactRepeating(type, System.currentTimeMillis(), interval, pi);
this will call reciver and reciver is,
public class AlarmReceiver extends BroadcastReceiver {
Context context;
#Override
public void onReceive(Context context, Intent intent) {
this.context = context;
System.out.println("Alarma Reciver Called");
if (isMyServiceRunning(this.context, BackgroundService.class)) {
System.out.println("alredy running no need to start again");
} else {
Intent background = new Intent(context, BackgroundService.class);
context.startService(background);
}
}
public static boolean isMyServiceRunning(Context context, Class<?> serviceClass) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
if (services != null) {
for (int i = 0; i < services.size(); i++) {
if ((serviceClass.getName()).equals(services.get(i).service.getClassName()) && services.get(i).pid != 0) {
return true;
}
}
}
return false;
}
}
And this Alaram reciver calls once when android app is opened and when app is closed.SO the service is like this,
public class BackgroundService extends Service {
private String LOG_TAG = null;
#Override
public void onCreate() {
super.onCreate();
LOG_TAG = "app_name";
Log.i(LOG_TAG, "service created");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(LOG_TAG, "In onStartCommand");
//ur actual code
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
// Wont be called as service is not bound
Log.i(LOG_TAG, "In onBind");
return null;
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.i(LOG_TAG, "In onTaskRemoved");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.i(LOG_TAG, "In onDestroyed");
}
}
I want my app to show a notification with a "GOOD MORNING" message at 6 A.M everyday. As I read, for this I need the app to run in background so I need to use Service.
I have tried the following code but I'm stuck.
MainActivity.java
public void onClickStartService(View v)
{
startService(new Intent(this,MyService.class));
}
public void onClickStopService(View v)
{
stopService(new Intent(this,MyService.class));
}
and MyService.java is
public class MyService extends Service{
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
#Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
//Note: You can start a new thread and use it for long background processing from here.
}
#Override
public void onDestroy() {
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
}
I have buttons to start and stop the Service and it works. Now I want the service to create notification as I have mentioned at the beginning of the post. How can I do this?
To start the service at a specific time, I suggest you create a BroadcastReceiver triggered by an Alarm, which will in turn start your service.
First write a BroadcastReceiver like this :
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
context.startService(new Intent(context, MyService.class));
}
/**
* Schedule the next update
*
* #param context
* the current application context
*/
private static void scheduleServiceUpdates(final Context context) {
// create intent for our alarm receiver (or update it if it exists)
final Intent intent = new Intent(context, AlarmReceiver.class);
final PendingIntent pending = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// compute first call time 1 minute from now
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, 10);
long trigger = calendar.getTimeInMillis();
// set delay between each call : 24 Hours
long delay = 24 * 60 * 60 * 1000;
// Set alarm
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, trigger, delay, pending);
// you can use RTC_WAKEUP instead of RTC to wake up the device
}
}
Then you just need to call the scheduleServiceUpdate method to start the reccuring event. If you only use the RTC type, then if the phone is locked when the alarm should trigger the service, it won't and will wait until the device is unlocked by the user. If you use RTC_Wakeup, the service will start exactly at the time given.
Note that there are other methods in the AlarmManager to trigger events.
You can start from PendingIntent and AlarmManager
Tutorial here
Dont forget to add possibility to cancel alarm manager with
mAlarmManager.cancel(pendingIntent);
Also you may want to intercept android.intent.action.BOOT_COMPLETED event to make you app starting immediately after reboot if you want to start your service by schedule.
I'm working on an Android app that sues a background task to fetch XML from a URL every x seconds (user defined interval, defaulting to 60). My structure is as so:
MainActivity
This schedules an Alarm via AlarmManager:
public static void scheduleAlarm(Context voContext, int viCheckInterval)
{
try {
moAlarmManager = (AlarmManager) voContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(voContext, AlarmReceiver.class);
moAlarmIntent = PendingIntent.getBroadcast(voContext, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.add(Calendar.SECOND, viCheckInterval);
moAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
time.getTimeInMillis(), moAlarmIntent);
} catch (Exception e) {
Log.e("MessageCheckAlarmHandler", e.toString());
}
}
AlarmReceiver
This is a BroadcastReceiver that starts the service:
#Override
public void onReceive(Context context, Intent intent)
{
Context oAppContext = context.getApplicationContext();
if (oAppContext == null) {
oAppContext = context;
}
Intent serviceIntent = new Intent(oAppContext, MessagingService.class);
oAppContext.startService(serviceIntent);
}
MessagingService
This creates our in-house logger (logging over TCP) and starts an AsyncTask called FetchPageTask:
public class MessagingService extends Service
{
#Override
public void onCreate()
{
super.onCreate();
...
this.acquireLocks();
try {
String sCheckerUrl = oPreferences.getString("pref_server", "");
int sCheckerPort = Integer.parseInt(oPreferences.getString("pref_server_port",
"8050"));
sCheckerUrl = String.format(URL, sCheckerUrl, sCheckerPort);
this.moFetchInboxTask = new FetchPageTask(this.logger, this);
this.moFetchInboxTask.execute(sCheckerUrl);
} finally {
this.releaseLocks();
this.stopSelf();
}
}
#Override
public void onDestroy()
{
super.onDestroy();
this.logger.close();
}
#Override
public IBinder onBind(Intent intent)
{
return null;
}
/**
* Acquire a WakeLock and a WifiLock.
*/
private void acquireLocks()
{
try {
// Acquire a wake lock to prevent the device from entering "deep sleep"
PowerManager oPowerManager = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
this.moWakeLock = oPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
this.moWakeLock.acquire();
// Acquire a WiFi lock to ensure WiFi is enabled
WifiManager wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
this.moWifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
this.moWifiLock.acquire();
} catch (Exception e) {
this.logger.error(TAG + "->onCreate()", "Error acquiring locks: " + e.toString());
}
}
/**
* Release our WakeLock and WifiLock.
*/
private void releaseLocks()
{
try {
this.moWakeLock.release();
this.moWifiLock.release();
} catch (Exception e) {
this.logger.error(TAG + "->releaseLocks()", e.toString());
}
}
}
FetchPageTask
This extends AsyncTask and does all the work of fetching the page and parsing the XML. It also then adds notifications and performs actions on the data retreived if need be.
This all works fine once, but does not run subsequently. I know that the AsyncTask works as I was previously doing it using pure Java via ScheduledExecutorService and ScheduledFuture and it worked. The only reason I decided to change to using AlarmManager is for maintainability purposes.
First, you are setting up your alarm to occur immediately, and then every ~43 years thereafter. That is unlikely to be what you want. Modify the third parameter of your call to setRepeating() to be the desired period in milliseconds, which right now is set to the number of milliseconds since midnight 1 January 1970.
Second, you are acquiring your WakeLock too late. There is no guarantee that your acquireLocks() will get a chance to run before the device falls asleep. My WakefulIntentService or the new WakefulBroadcastReceiver offer better patterns for passing control to an IntentService.
I think you don't need the Calendar here. You just want to run the action every x seconds, so It would be something like this:
moAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
viCheckInterval, moAlarmIntent);
// viCheckInterval should be long miliseconds