由于项目的需要现在这里做了一个小小的闹钟 过多的解释就不说了,看看源码吧
Button setTime;
AlarmManager amanager;
Calendar calendarTime=Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTime=(Button) findViewById(R.id.time);
amanager=(AlarmManager) getSystemService(Service.ALARM_SERVICE);
setTime.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Calendar calendarTime=Calendar.getInstance();
new TimePickerDialog(MainActivity.this, 0,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this, Alarm.class);
PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
Calendar c=Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR, arg1);
c.set(Calendar.MINUTE, arg2);
amanager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),pi);
Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();
}
}, calendarTime.get(Calendar.HOUR_OF_DAY), calendarTime.get(Calendar.MINUTE),false).show();
}
});
}
看到红色的字体嘛 嗯 对 他是另一个类
MediaPlayer player;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
player=MediaPlayer.create(this, R.raw.dis);
player.setLooping(true);
player.start();
new AlertDialog.Builder(Alarm.this).setTitle("Alarm")
.setMessage("Let's go!")
.setPositiveButton("yes", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
player.stop();
Alarm.this.finish();
}
}).show();
}
谢谢