【发布时间】:2017-07-24 00:34:13
【问题描述】:
我正在使用AlarmManager 在我的应用程序中安排事情,用户将在其中选择时间,我将我的服务类的待处理intent 传递给AlarmManager,这应该会在屏幕显示特定时间后触发警报已开启。
可以正常使用,但是锁屏时不触发报警。
我在我的服务中使用了 wakelock 和 partial wake lock 选项,但它不起作用。当我使用完全唤醒锁时它可以正常工作,那么部分锁定选项有什么问题?
代码贴在下面。
public void schedule(View v) {
AlarmManager localAlarmManager = (AlarmManager)getSystemService("alarm");
Calendar localCalendar = Calendar.getInstance();
localCalendar.set(Calendar.HOUR_OF_DAY, 12);
localCalendar.set(Calendar.MINUTE, 10);
localCalendar.set(Calendar.SECOND, 0);
Intent localIntent = new Intent(getBaseContext(), Backupservice.class);
localIntent.putExtra("startservice", "true");
PendingIntent localPendingIntent = PendingIntent.getService(getBaseContext(), 15, localIntent, 134217728);
localAlarmManager.cancel(localPendingIntent);
long l = localCalendar.getTimeInMillis();
System.out.println("schtm:" + localCalendar.getTimeInMillis() +"currenttm:"+System.currentTimeMillis());
localAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, l,1800000, localPendingIntent);
}
public class Backupservice extends Service {
public Backupservice(){
// cnt=context;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
miscallsettings=getSharedPreferences("MyPref", MODE_PRIVATE);
Log.i("Backupservice", "Service created");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
super.onStartCommand(intent, flags, startId);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
// PowerManager.ACQUIRE_CAUSES_WAKEUP |
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK |
PowerManager.ON_AFTER_RELEASE, "ggg");
wl.acquire();
save();
return super.onStartCommand(intent, flags, startId);
}
@SuppressWarnings("unchecked")
private void save() {
try {
// here I am writing the logic
wl.release();
} catch(Exception e) {
}
}
@Override
public void onDestroy() {
try {
wl.release();
} catch(Exception e) {
}
}
}
【问题讨论】:
-
显示您的 Backupservice.java 文件(您开始警报活动的位置)。
-
对不起,我没听明白。如果你问的是 Backupservice 课程,我已经发布了
-
我是说让我开始关注警报警报活动的意图。
-
检查这个意图 localIntent = new Intent(getBaseContext(), Backupservice.class);在时间表()中
-
你应该在调用服务时添加唤醒锁。这是一篇关于如何使用 Service 和 AlarmManager 的 blob 帖子:Using IntentService With AlarmManager to Schedule Alarms
标签: android alarmmanager