android的闹钟服务,
AlarmManageralarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
设置闹钟
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, ad.getTime(), 5*60*1000,
PendingIntent.getBroadcast(getContext(), ad.getId(),new Intent(getContext(), AlarmReceiver.class),0));
取消闹钟
alarmManager.cancel(PendingIntent.getBroadcast(getContext(), ad.getId(),new Intent(getContext(), AlarmReceiver.class),0));
闹钟的响应广播:
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiverextends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("闹钟执行了");
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(PendingIntent.getBroadcast(context, getResultCode(),new Intent(context, AlarmReceiver.class),0));
Intent i = new Intent(context, PlayAlarmAty.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
public class PlayAlarmAty extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_player_aty);
mp = MediaPlayer.create(this, R.raw.music);
mp.start();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
mp.stop();
mp.release();
}
private MediaPlayer mp;
}