在我的应用程序中,我有一个警报,它触发了一项从互联网下载信息并显示通知的服务。
这是我的代码的简化版本:
MyActivity 包含以下内容:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 20);
Intent intent = new Intent(this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20000, pendingIntent);
AlarmService 看起来像这样:
public class AlarmService extends Service {
@Override
public void onCreate() {
new myAsyncTask().execute();
}
private class myAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... args) {
//Download stuff
return null;
}
@Override
protected void onPostExecute(Void arg) {
//Show notification
}
}
}
我真的不明白什么时候使用唤醒锁,所以我的问题是:在这种情况下,我应该使用唤醒锁吗?如果是,我应该在哪里开始和停止它?
提前致谢