我为我的应用程序提供了一项服务,该服务基本上可以解析 X 个 RSS 提要。大多数提要都相当小,但有些可能只有 1MB 左右。我在我的服务中的 AsyncTask 中解析它们。解析可能相当耗时,具体取决于用户的连接和提要的数量,但我会说最多不超过一两分钟。在服务中使用 WakeLock 是个好主意吗?
这是我的代码,到目前为止。不确定这是否是设置服务的最佳方式:
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();
}
}