最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
基于Alarmmanager实现简单闹钟功能
时间:2022-06-25 22:55:58 编辑:袖梨 来源:一聚教程网
本文实例为大家分享了Alarmmanager实现简单闹钟功能的具体代码,供大家参考,具体内容如下
代码:
activity_main.xml
MainActivity.java
package com.example.haige.alarmmanager; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /* 启动闹钟 */ public void startAlarmClick(View view) { //获取系统的闹钟服务 AlarmManager am= (AlarmManager) getSystemService(Context.ALARM_SERVICE); //触发闹钟的时间(毫秒) long triggerTime= System.currentTimeMillis()+3000; Intent intent=new Intent(this,Alarmctivity.class); PendingIntent op=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); //启动一次只会执行一次的闹钟 am.set(AlarmManager.RTC,triggerTime,op); // //指定时间重复执行闹钟 // am.setRepeating(AlarmManager.RTC,triggerTime,2000,op); } /* 设置闹钟 */ public void startSetAlarmClick(View view) { //获取系统的闹钟服务 AlarmManager am= (AlarmManager) getSystemService(Context.ALARM_SERVICE); //触发闹钟的时间(毫秒) long triggerTime= System.currentTimeMillis()+3000; Intent intent=new Intent(this,AlarmReceiver.class); PendingIntent op=PendingIntent.getBroadcast(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT); //启动一次只会执行一次的闹钟 am.set(AlarmManager.RTC,triggerTime,op); // //指定时间重复执行闹钟 // am.setRepeating(AlarmManager.RTC,triggerTime,2000,op); } }
activity_alarmctivity.xml
Alarmctivity.java
package com.example.haige.alarmmanager; import android.app.Activity; import android.app.AlarmManager; import android.app.AlertDialog; import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import java.io.IOException; public class Alarmctivity extends Activity { MediaPlayer mp; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alarmctivity); mp = new MediaPlayer(); try { mp.setDataSource(this, Uri.parse("/storage/sdcard1/kugou/听力/南拳妈妈-你不像她.mp3")); mp.prepare(); } catch (IOException e) { e.printStackTrace(); } mp.setLooping(true); mp.start(); alarmOialog(); } @Override protected void onResume() { super.onResume(); } @Override protected void onDestroy() { super.onDestroy(); if(mp!=null) { if(mp.isPlaying()) { mp.stop(); } mp.release(); } } public void alarmOialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("大哥,大妹子喊你起床啦!"); builder.setPositiveButton("再来一次", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { alarm(); finish(); } }); builder.setNegativeButton("停止", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish();//关闭窗口 } }); builder.show(); } private void alarm() { //获取系统的闹钟服务 AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); //触发闹钟的时间(毫秒) long triggerTime = System.currentTimeMillis() + 5000; Intent intent = new Intent(this, Alarmctivity.class); PendingIntent op = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); //启动一次只会执行一次的闹钟 am.set(AlarmManager.RTC, triggerTime, op); //指定时间重复执行闹钟 // am.setRepeating(AlarmManager.RTC,triggerTime,2000,op); } }
广播接收器AlarmReceiver.java
package com.example.haige.alarmmanager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class AlarmReceiver extends BroadcastReceiver { public AlarmReceiver() { } @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"起床啦,起床啦!",Toast.LENGTH_SHORT).show(); } }
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22