Android定时闹钟与定时情景模式

文章目录

    • 1.定时闹钟
        • 单次闹钟
        • 重复闹钟
    • 2.定时情景模式切换

1.定时闹钟

单次闹钟

单次闹钟只要响音button,设置sender跳转至OnShotAlarm广播,通过alarmmanager.set设置单次单次闹钟,响应sender。

private View.OnClickListener mOneShortLisener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            start_Music = 0;
            Intent intent = new Intent(main.this, OneShotAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(main.this, 0, intent, 0);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 5);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
        }
    };

重复闹钟

设置重复闹钟的操作和单次闹钟差别不大,只是要设置重复的间隔,再利用alarmmanager.setRepeating响音重复的广播跳转。

 private View.OnClickListener mStartReaptingLisener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            start_Music=0;
            Intent intent = new Intent(main.this, RepeatingAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(main.this, 0, intent, 0);
            long firstTime = SystemClock.elapsedRealtime();
            firstTime += 1000;
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 1000, sender);
        }
    };

取消重复闹钟,只要利用alarmmanager.cancel即可。

private View.OnClickListener mStopReaptingLisener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(main.this, RepeatingAlarm.class);
            PendingIntent sender = PendingIntent.getBroadcast(main.this, 0, intent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.cancel(sender);
        }
    };

2.定时情景模式切换

(作为一只Android小白,我感觉我的做法好水)

用EditText获取用户所要循环的周期(没有用TimePicker,感觉很不人性话,以后会改进的),然后用RadioGroup设置单选,最后在主活动中获取,定时还是借助强大的AlarmManager,然后作为小白的我竟然忽略了一个严重的问题,BroadcastReceiver类不是View类,不能直接对RadioGroup操作于是。。。。:

在主活动中获取RadioGroup,然后通过Intent传值处理。

//读取RadioGroup
RadioGroup radioGroup = findViewById(R.id.radiogroup);
            if(Contrasl_Visal==0)
            ((RadioButton)radioGroup.getChildAt(0)).setChecked(true);
            Contrasl_Visal=+1;
            int changeId = 0;
            int m=0;
            for(int i=0;i<radioGroup.getChildCount();i++)
            {
                RadioButton radioButton= (RadioButton) radioGroup.getChildAt(i);
                if(radioButton.isChecked())
                {
                    changeId=m;
                    break;
                }
                m=m+1;
            }
            Intent intent= new Intent(main.this,AlarmReceiver.class);
            intent.putExtra("changId",changeId);
//通过传值分析用户所选的情景模式,然后直接用AudioManager改变情景模式。
 public void onReceive(Context context, Intent intent) {
        int changeId=0;
        Intent intent1= new Intent();
        intent1.putExtra("changId",changeId);
        switch (changeId)
        {
            case RINGER_MODE_NORMAL1:
            {
                AudioManager audioManager= (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                audioManager.setStreamVolume(AudioManager.RINGER_MODE_NORMAL,50,0);
            }break;
            case RINGER_MODE_VIBRATE1:
            {
                AudioManager audioManager= (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            }break;
            case RINGER_MODE_SILENT1:
            {
                AudioManager audioManager= (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            }break;
        }
    }

注意: 在这中间遇到了很多空指针问题。
RadioGroup radioGroup = findViewById(R.id.radiogroup);
最后发现是我把他写在了最前面,在RadioGroup没有输入的时候就给读取了,当然是空的了,只得给他初始化,默认第一个RadioButton选中。((RadioButton)radioGroup.getChildAt(0)).setChecked(true);

虽然是一个非常简单的项目,然而没有熟练运用debug的我还是费了点功夫,从中更是学到了许多学习基本知识无法得到的经验,明白了做项目才能锻炼出能力,我会多加练习。小白进阶中。。。

你可能感兴趣的:(Android小白成长之路)