设备锁定时是否可以播放声音?

时间:2014-09-23 21:53:17

标签: javascript android cordova

我正在使用phonegap为我的Android设备制作一个闹钟应用程序,我想知道当设备被锁定且计时器用完时是否可以播放声音

1 个答案:

答案 0 :(得分:0)

是。在Android上,执行此操作以按实时时钟安排唤醒:

private static final long WINDOW_MS = 50L; // Allow some time flexibility to save battery power.

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = makeAlarmPendingIntent(context);
long nextReminder = ... // the SystemClock.elapsedRealtime() for the next reminder notification
alarmMgr.setWindow(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextReminder - WINDOW_MS,
                   WINDOW_MS, pendingIntent);

使用BroadcastReceiver来接收此pendingIntent。当相关的Intent到达时,请使用Notification播放声音。请参阅NotificationCompat.Builder及其setSound()方法。

Cordova可能会提供所需的API来执行此操作。如果没有,您必须在应用程序的Java部分实现它。

相关问题