Reputation: 3578
I have a service for my app that basically parses an X number of RSS feeds. Most of the feeds are fairly small, but some can be around 1MB in size. I'm parsing them in an AsyncTask in my service. The parsing can be fairly time consuming depending on the user's connection and number of feeds, but I'd say no more than a minute or two at most. Is it a good idea to use a WakeLock in the service?
This is my code, so far. Not sure if this is the best way to set up the service or not:
public class MainService extends Service
{
private WakeLock mWakeLock;
@Override
public void onCreate()
{
handleIntent();
}
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId)
{
return START_STICKY;
}
@Override
public void onStart(final Intent intent, final int startId) {
handleIntent();
}
protected void handleIntent()
{
// obtain the wake lock
final PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
mWakeLock.acquire();
if (!((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE)).getBackgroundDataSetting())
{
stopSelf();
return;
}
new AsyncParsingTask().execute();
}
@Override
public void onDestroy()
{
super.onDestroy();
mWakeLock.release();
}
}
Upvotes: 2
Views: 550
Reputation: 1006704
Is it a good idea to use a WakeLock in the service?
Yes, if having the device fall asleep could pose a problem to you. I would recommend that you use my WakefulIntentService
, though, as there are many tricky cases in dealing with WakeLock
objects. You also would not need to mess with your own AsyncTask
or stopSelf()
, as those would be handled for you by IntentService
(from which WakefulIntentService
inherits).
Upvotes: 2
Reputation: 2605
Service runs in background there is no point of acquireing wakelock there however what you can do is that check if your activity is running and in that condition acquire the wake lock and release when activity is finished
Upvotes: 0