赞
踩
本篇通过AlarmManager和PendingIntent实现定时提醒功能,界面运用Button、TextView、Switch进行布局
同时这也是中国大学慕课移动终端应用开发的网课作业18,我会持续更新我的作业,如果有需要关注一下吧
具体的组件介绍请点击上面链接,观看课程内详细信息,这里不多赘述,只贴我实现的代码。
代码包括时间设置Activity—TimePickerActivity和其对应布局以及闹钟Activity—MyClockActivity
同时记得注册Activity
<uses-permission android:name="android.permission.VIBRATE" />
public class TimePickerActivity extends AppCompatActivity { private Button mButtonChooseTime,mButtonStart,mButtonCancel; private TextView mTextView; private Switch mSwitch; private boolean flag = false;//是否应该震动 private PendingIntent mPendingIntent; private AlarmManager mAlarmManager; private Intent mIntent; private int requestCode = 0; private Calendar mCalendar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_time_picker); init(); mButtonChooseTime.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //用于存放当前时间 Calendar currentTime = Calendar.getInstance(); new TimePickerDialog(TimePickerActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mCalendar = Calendar.getInstance(); long systemTime = System.currentTimeMillis();//取出当前的时间毫秒值 mCalendar.set(Calendar.HOUR,hourOfDay); mCalendar.set(Calendar.MINUTE,minute); mCalendar.set(Calendar.SECOND,0); mCalendar.set(Calendar.MILLISECOND,0); long setTime = mCalendar.getTimeInMillis(); if (systemTime>setTime){ mCalendar.add(Calendar.DAY_OF_MONTH,1); } mTextView.setText("您选择了"+hourOfDay+"时"+minute+"分"); } }, currentTime.get(Calendar.HOUR_OF_DAY), currentTime.get(Calendar.MINUTE), false).show(); } }); mButtonStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); mIntent = MyClockActivity.createIntent(TimePickerActivity.this,flag); mPendingIntent = PendingIntent.getActivity(TimePickerActivity.this,requestCode,mIntent,PendingIntent.FLAG_UPDATE_CURRENT); //设置闹铃 mAlarmManager.setExact(AlarmManager.RTC,mCalendar.getTimeInMillis(),mPendingIntent); mButtonStart.setEnabled(false); mButtonCancel.setEnabled(true); } }); mButtonCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAlarmManager.cancel(mPendingIntent); mButtonStart.setEnabled(true); mButtonCancel.setEnabled(false); } }); mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { flag = isChecked; mButtonStart.setEnabled(true); mButtonCancel.setEnabled(false); } }); } private void init(){ mButtonChooseTime = findViewById(R.id.time_picker_button_choose_time); mButtonStart = findViewById(R.id.time_picker_button_start); mButtonCancel = findViewById(R.id.time_picker_button_cancel); mTextView = findViewById(R.id.time_picker_text_show_info); mSwitch = findViewById(R.id.time_picker_switch_choose); mButtonStart.setEnabled(false); mButtonCancel.setEnabled(false); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/time_picker_text_show_info" android:layout_gravity="center" android:layout_marginTop="30dp" android:textSize="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/time_picker_button_choose_time" android:layout_gravity="center" android:layout_marginTop="30dp" android:text="选择时间" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:layout_gravity="center" android:layout_marginTop="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:text="是否震动 " android:textSize="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Switch android:id="@+id/time_picker_switch_choose" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <Button android:layout_gravity="center" android:layout_marginTop="30dp" android:id="@+id/time_picker_button_start" android:text="开启定时" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:layout_gravity="center" android:layout_marginTop="30dp" android:id="@+id/time_picker_button_cancel" android:text="取消定时" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
public class MyClockActivity extends AppCompatActivity { private MediaPlayer mMediaPlayer; private boolean flag; private Vibrator mVibrator; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_clock); //获取传递过来的信息 flag = getIntent().getBooleanExtra("flag",false); mMediaPlayer = MediaPlayer.create(this,R.raw.music); mMediaPlayer.start(); // 弹出一个闹钟提醒对话框---关闭闹钟,让播放闹钟的media play停止播放声音 new AlertDialog.Builder(this) .setTitle("闹钟") .setMessage("时间到了") .setPositiveButton("关闭闹钟", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mMediaPlayer.stop(); if (flag){ mVibrator.cancel(); } MyClockActivity.this.finish(); } }).show(); if (flag){ //震动 mVibrator = (Vibrator) MyClockActivity.this.getSystemService(MyClockActivity.VIBRATOR_SERVICE); long [] patter = {1000, 1000, 2000, 50}; mVibrator.vibrate(patter, 0); } } public static Intent createIntent(Context context,boolean flag){ Intent intent = new Intent(context,MyClockActivity.class); intent.putExtra("flag",flag); return intent; } }
在res下新建一个raw文件夹,将音频文件放入,命名为music.xxx
如果有什么问题,请私信联系我或者在评论区留言
码字不易,若有帮助,给个关注和赞呗
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。