您可以像这样使用pendingIntent
a BroadCastReceiver
:
Calendar cal = Calendar.getInstance();
Intent activate = new Intent(context, Alarm.class);
AlarmManager alarms ;
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, activate, 0);
alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 00);
alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
然后是广播接收器:
public class Alarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MediaPlayer mp = MediaPlayer.create(context, R.raw.ferry_sound);
mp.start();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire();
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
long[] s = { 0, 100, 10, 500, 10, 100, 0, 500, 10, 100, 10, 500 };
vibrator.vibrate(s, -1);
}
}
不要忘记将这些权限包含在您的AndroidManifest.xml
:
....
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
....
和应用程序标签中的广播接收器:
<receiver android:process=":remote" android:name="Alarm" />