扣丁学堂——AlarmManager

一丶源码见本文

二丶课程讲解及代码

扣丁学堂——AlarmManager_第1张图片


扣丁学堂——AlarmManager_第2张图片

扣丁学堂——AlarmManager_第3张图片

扣丁学堂——AlarmManager_第4张图片

MainActivity

ublic class Main1217Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1217);
    }
    public  void  startSet(View v){
        //获取到了系统的闹钟服务
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        //触发闹钟的时间
        long triggerTime =  System.currentTimeMillis()+3000;


        Intent intent  = new Intent(this,Main2Activity2.class);

// 可以get 广播,服务,activity 类型
        PendingIntent op = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        //只会执行一次的闹钟
        am.set(AlarmManager.RTC, triggerTime, op); //op : 启动这个闹钟的操作,也就是干什么事.

    }

    public  void startAlarmClick(View v){

        //获取到了系统的闹钟服务
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        //触发闹钟的时间
        long triggerTime =  System.currentTimeMillis()+3000;


        Intent intent  = new Intent(this,MyReceiverR.class);

// 可以get 广播,服务,activity 类型
        PendingIntent op = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        //只会执行一次的闹钟
        am.set(AlarmManager.RTC,triggerTime,op); //op : 启动这个闹钟的操作,也就是干什么事.

        //指定时间重复执行
       // am.setRepeating(AlarmManager.RTC, triggerTime, 2000, op); //2000 : 间隔时间,多长时间重复一次

        //am.cancel(op);//取消操作

    }
}

Activity2

public class Main2Activity2 extends AppCompatActivity {

    MediaPlayer mp; //媒体播放对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //唤醒屏幕
        Window win = getWindow();
        win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        setContentView(R.layout.activity_main22);

        mp = new MediaPlayer();
        try {
            mp.setDataSource(this, Uri.parse("/storage/extSdCard/qqmusic/song/a.mp3"));
            mp.prepare(); //状态,预备状态
        } catch (IOException e) {
            e.printStackTrace();
        }

        mp.setLooping(true); //循环
        mp.start();  //启动
        alarmDialog();  //弹出对话框

    }





    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mp !=null){
            if(mp.isPlaying()){
                mp.stop();
            }

            mp.release(); //释放资源
        }
    }

    public  void alarmDialog(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage("起床了"); //弹出框提示内容

        //弹出框按钮
        builder.setPositiveButton("再来一次", new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                alarm();//由闹钟服务来自动 启动对话窗口

                finish();//关闭窗口
            }
        });

        builder.setNegativeButton("关闭了", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                finish();
            }
        });

        builder.show();
    }



    private  void alarm(){
        //获取到了系统的闹钟服务
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        //触发闹钟的时间
        long triggerTime =  System.currentTimeMillis()+5000;


        Intent intent  = new Intent(this,Main2Activity2.class);

        PendingIntent op = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        //只会执行一次的闹钟
        am.set(AlarmManager.RTC, triggerTime, op); //op : 启动这个闹钟的操作,也就是干什么事.


    }

}

Receive

public class MyReceiverR extends BroadcastReceiver {
    public MyReceiverR() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "闹钟", Toast.LENGTH_SHORT).show();

    }
}


你可能感兴趣的:(扣丁学堂——AlarmManager)