定时闹钟功能(带铃声 可延时)

项目中需要写一个小闹钟提醒用户。

 时间选择器使用的是 TimePickerDialog

    private AlarmManager alarm; // 闹钟管理
    private Calendar calendar = Calendar.getInstance() ;//取得Calender对象


 new TimePickerDialog(getContext(), 0, new TimePickerDialog.OnTimeSetListener() {
               @Override
               public void onTimeSet(TimePicker timePicker, int i, int i1) {
                   hour=i;
                   min=i1;
                   calendar.setTimeInMillis(System.currentTimeMillis());//设置当前时间
                   calendar.set(Calendar.HOUR_OF_DAY,hour);
                   calendar.set(Calendar.MINUTE,min);
                   calendar.set(Calendar.SECOND,0);
                   calendar.set(Calendar.MILLISECOND,0);
                   
                   Intent intent=new Intent(getContext(),SampleBootReceiver.class);//指定跳转的Intent
                   intent.setAction("org.campass.action.setalarm") ;//定义广播的Action
                   PendingIntent sender=PendingIntent.getBroadcast(getContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
                   showNotification(i,i1);
                   try {
                       alarm.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),sender);//设置闹钟
                       Toast.makeText(getContext(),"设置闹钟成功",Toast.LENGTH_SHORT).show();
                   }catch (Exception e){
                       Toast.makeText(getContext(),"设置闹钟失败",Toast.LENGTH_SHORT).show();
                   }


               }
           },hour,min,true).show();

然后在跳转的 SampleBootReceiver 指定需要跳转的Activity

public class SampleBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent it = new Intent(context, AlarmActivity.class);//定义要操作的Intent
        it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//传递一个新的任务标记
        context.startActivity(it);//启动Intent
    }
}

同时需要在AndroidManifest.xml 注册

    //.配置广播器
            
                
            
        

最后写AlarmActivity 闹钟的 提示框和铃声

public class AlarmActivity extends Activity {
  MediaPlayer mPlayer;
  private AlertDialog mAlertDialog;
  private AlertDialog.Builder mBuilder=null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        alarmMusic_play();
        mAlertDialog=null;
        mBuilder=new AlertDialog.Builder(this);
        mAlertDialog=mBuilder.setIcon(R.drawable.ic_launcher_background)
                .setTitle("这是闹钟标题")
                .setMessage("这是闹钟内容")
                .setNegativeButton("延迟一小时", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        alarmMusic_stop();
                        alarmdelay();
                        AlarmActivity.this.finish();
                    }
                }).setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        alarmMusic_stop();
                          //关闭通知
                        NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                        manager.cancel(1);
                        AlarmActivity.this.finish();
                    }
                }).create();//创建
        mAlertDialog.show();
    }
    private void alarmMusic_play(){
        //加载指定音乐,并为之创建MediaPlayer对象
        mPlayer=MediaPlayer.create(this,R.raw.shape);
        mPlayer.setLooping(true);
        //播放闹钟
        mPlayer.start();
    }
    private void alarmMusic_stop(){
        //停止音乐
        mPlayer.stop();
        if (mManager!=null){
            mManager.cancel(alarmIntent);
        }
    }
    private AlarmManager mManager;
    private PendingIntent alarmIntent;
   
/*
 延时一个小时
*/
    private void alarmdelay(){
        mManager= (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        Intent intent=new Intent(this,MainActivity.class);
        alarmIntent=PendingIntent.getBroadcast(this,0,intent,0);
        Calendar calendar=Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        int hour=calendar.get(Calendar.HOUR);
        int min=calendar.get(Calendar.MINUTE);
        calendar.set(Calendar.HOUR,hour);
        calendar.set(Calendar.MINUTE,min);
        mManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*60,alarmIntent);
    }
}

最后一定要在AndroidManifest.xml中 写权限

 

你可能感兴趣的:(定时闹钟功能(带铃声 可延时))