本篇通过AlarmManager和PendingIntent实现定时提醒功能,界面运用Button、TextView、Switch进行布局 同时这也是中国大学慕课移动终端应用开发的网课作业18,我会持续更新我的作业,如果有需要关注一下吧 具体的组件介绍请点击上面链接,观看课程内详细信息,这里不多赘述,只贴我实现的代码。 代码包括时间设置Activity—TimePickerActivity和其对应布局以及闹钟Activity—MyClockActivity 同时记得注册Activity 在res下新建一个raw文件夹,将音频文件放入,命名为music.xxx 如果有什么问题,请私信联系我或者在评论区留言说明
界面展示
代码部分
1.先在AndroidManifest.xml中设置权限
<uses-permission android:name="android.permission.VIBRATE" />
2.TimePickerActivity.java
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); } }
2.activity_time_picker.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://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>
3.MyClockActivity.java
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; } }
铃声资源
总结
码字不易,若有帮助,给个关注和赞呗
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算