문제

I'm rather wondering if this is specific to my device (Nexus 4, Android 4.3, stock ROM), but I have an alarm, registered once via the AlarmManager. When triggered, the device is set to vibrate for two seconds. If the alarm is triggered while the device is on, then it correctly vibrates for the two seconds. However, if the alarm is triggered when the device is off (and unplugged), the vibration starts, but doesn't stop until the power button is pressed (to wake up the device). Here is the code to register the alarm:

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);
}

And the receiver code:

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);
    }
}

Any ideas on why this is happening or how to prevent it?

도움이 되었습니까?

해결책

Finally figured this one out. My innocuous 'Toast' which gets triggered before the vibration was causing the vibrator to hang until the toast was complete. The Toast wasn't showing until the screen was turned on and this somehow blocked the completion of the vibration. Removing the Toast solved my issue and the vibration ceased after two seconds as expected even with the screen off.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top