无法将 AlarmManager 设置为重复一个
Can't set AlarmManager to repeating one
我正在尝试将闹钟设置为每 30 秒唤醒一次,但它不起作用。它在开始时只执行 1 次。我不确定我是否必须使用 setInexactRepeating 或 setRepeating 或 setExact(并回忆一下方法);如果我应该使用 ELAPSED_REALTIME_WAKEUP 或 RTC_WAKEUP.
public void onClick(View view) {
alarmMethod(this);
}
public static void alarmMethod(Context context) {
Intent myIntent = new Intent(context, LocalWordService.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(context, 0 ,myIntent, 0);
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE, cur_cal.get(Calendar.MINUTE) + 1);
cal.set(Calendar.SECOND, 30);
alarmManager.cancel(pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000 * 30, pendingIntent);
Log.d("MainActivity", "Alarm at = " + cal.getTime().toString());
Toast.makeText(context, "Start Alarm", Toast.LENGTH_SHORT).show();
}
我的服务是:
public class LocalWordService extends Service {
@Override
public IBinder onBind(Intent arg0) {
Toast.makeText(getApplicationContext(), "onBind", Toast.LENGTH_SHORT).show();
return null;
}
@Override
public void onCreate(){
Toast.makeText(getApplicationContext(), "WAKE UP!!!", Toast.LENGTH_SHORT).show();
Log.d("Service", "ActualTime : " + DateFormat.getDateInstance().getCalendar().getTime().toString());
//MainActivity.alarmMethod(getApplicationContext());
}
}
PS:我正在使用 Android Lollipop 设备进行测试。 (5.0.2)
put your code from onCreate()
to onStartCommand()
因为我认为您在 onReceive()
中使用了 startService()
的警报接收器。并且它第一次创建您的服务并调用 onCreate()
但是在调用 startService()
30 秒后,服务已经创建,因此它调用 onStartCommand()
.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "WAKE UP!!!", Toast.LENGTH_SHORT).show();
Log.d("Service", "ActualTime : " + DateFormat.getDateInstance().getCalendar().getTime().toString());
//MainActivity.alarmMethod(getApplicationContext());
return super.onStartCommand(intent,flags,startId);
}
我正在尝试将闹钟设置为每 30 秒唤醒一次,但它不起作用。它在开始时只执行 1 次。我不确定我是否必须使用 setInexactRepeating 或 setRepeating 或 setExact(并回忆一下方法);如果我应该使用 ELAPSED_REALTIME_WAKEUP 或 RTC_WAKEUP.
public void onClick(View view) {
alarmMethod(this);
}
public static void alarmMethod(Context context) {
Intent myIntent = new Intent(context, LocalWordService.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(context, 0 ,myIntent, 0);
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE, cur_cal.get(Calendar.MINUTE) + 1);
cal.set(Calendar.SECOND, 30);
alarmManager.cancel(pendingIntent);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000 * 30, pendingIntent);
Log.d("MainActivity", "Alarm at = " + cal.getTime().toString());
Toast.makeText(context, "Start Alarm", Toast.LENGTH_SHORT).show();
}
我的服务是:
public class LocalWordService extends Service {
@Override
public IBinder onBind(Intent arg0) {
Toast.makeText(getApplicationContext(), "onBind", Toast.LENGTH_SHORT).show();
return null;
}
@Override
public void onCreate(){
Toast.makeText(getApplicationContext(), "WAKE UP!!!", Toast.LENGTH_SHORT).show();
Log.d("Service", "ActualTime : " + DateFormat.getDateInstance().getCalendar().getTime().toString());
//MainActivity.alarmMethod(getApplicationContext());
}
}
PS:我正在使用 Android Lollipop 设备进行测试。 (5.0.2)
put your code from
onCreate()
toonStartCommand()
因为我认为您在 onReceive()
中使用了 startService()
的警报接收器。并且它第一次创建您的服务并调用 onCreate()
但是在调用 startService()
30 秒后,服务已经创建,因此它调用 onStartCommand()
.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "WAKE UP!!!", Toast.LENGTH_SHORT).show();
Log.d("Service", "ActualTime : " + DateFormat.getDateInstance().getCalendar().getTime().toString());
//MainActivity.alarmMethod(getApplicationContext());
return super.onStartCommand(intent,flags,startId);
}