package com.yqy.yqy_alarm; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.text.style.ImageSpan; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class MainActivity extends Activity { private TextView tv = null; private Button setTime,cancelTime; private Calendar c = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String alarm = getIntent().getStringExtra("alarm"); if(alarm != null&&alarm.equals("alarm")){ stopService(new Intent("com.yqy.yqy_alarm.MUSIC")); } tv = (TextView)findViewById(R.id.tv); setTime = (Button)findViewById(R.id.setAlarm); cancelTime = (Button)findViewById(R.id.cancelAlarm); //得到日历实例,主要是为了下面的获取时间 c = Calendar.getInstance(); setTime.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { c.setTimeInMillis(System.currentTimeMillis()); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); new TimePickerDialog(MainActivity.this, minute, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { //是设置日历的时间,主要是让日历的年月日和当前同步 c.setTimeInMillis(System.currentTimeMillis()); //设置小时分钟,秒和毫秒都设置为0 c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); int requestCode = 0;//闹钟的唯一标示 Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); intent.putExtra("requestCode", requestCode); PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, requestCode, intent, 0); //得到AlarmManager实例 AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); //根据当前时间预设一个警报 am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi); /** * 第一个参数是警报类型;第二个参数是第一次执行的延迟时间,可以延迟,也可以马上执行;第三个参数是重复周期为一天 * 这句话的意思是设置闹铃重复周期,也就是执行警报的间隔时间 */ // am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(60*1000), // (24*60*60*1000), pi); // am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), // 1000*60*5, pi); String msg = hourOfDay+":"+minute; tv.setText("当前设置的闹钟时间:"+msg); } }, hour, minute, true).show(); //上面的TimePickerDialog中的5个参数参考:http://blog.csdn.net/yang_hui1986527/article/details/6839342 } }); cancelTime.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); //取消警报 am.cancel(pi); tv.setText("闹钟取消"); //取消闹钟的同时取消音乐 stopService(new Intent("com.yqy.yqy_alarm.MUSIC")); } }); } }
package com.yqy.yqy_alarm; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class AlarmReceiver extends BroadcastReceiver { Intent intent; int requestCode = -1; @Override public void onReceive(Context context, Intent arg1) { requestCode = arg1.getIntExtra("requestCode", 0); NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); Notification n = new Notification(R.drawable.ic_launcher, "Hello,there!", System.currentTimeMillis()); n.flags = Notification.FLAG_AUTO_CANCEL; intent = new Intent(context, MainActivity.class); intent.putExtra("alarm", "alarm"); intent.setComponent(new ComponentName("com.yqy.yqy_alarm", "com.yqy.yqy_alarm.MainActivity")); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步,设置启动模式 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); n.setLatestEventInfo( context, "Hello,there!", "Hello,there,I'm john.", contentIntent); nm.notify(R.string.app_name, n); Toast.makeText(context, "闹钟时间到", Toast.LENGTH_SHORT).show(); intent = new Intent("com.yqy.yqy_alarm.MUSIC"); //启动Service播放音乐 context.stopService(new Intent("com.yqy.yqy_alarm.MUSIC")); context.startService(intent); } }
package com.yqy.yqy_alarm; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; public class MusicService extends Service { private MediaPlayer player; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void onStart(Intent intent, int startId) { super.onStart(intent, startId); player = MediaPlayer.create(this, R.raw.beep); player.setLooping(true); player.start(); } public void onDestroy() { super.onDestroy(); if(player != null){ player.stop(); } } }
原文地址:http://blog.csdn.net/u012885461/article/details/46457955