您可以使用pendingIntent
與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" />