我想知道这是否特定于我的设备(Nexus 4、Android 4.3、库存 ROM),但我有一个警报,通过 AlarmManager 注册一次。触发时,设备设置为振动两秒钟。如果在设备开启时触发警报,则它会正确振动两秒钟。但是,如果在设备关闭(并拔下电源)时触发警报,则振动会开始,但直到按下电源按钮(以唤醒设备)才会停止。这是注册警报的代码:
public static void registerAlarm(Context context, int uniqueId, long triggerAlarmAt)
{
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction("com.myapp.ALARM_EVENT");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), uniqueId, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAlarmAt, pendingIntent);
}
和接收者代码:
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent arg1)
{
Toast.makeText(context, "Test of alarm", Toast.LENGTH_LONG).show();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
}
}
关于为什么会发生这种情况或如何防止它的任何想法?