Android中AlarmManager使用示例(持续更新)

欠伸展肢体,吟咏心自愉。这篇文章主要讲述Android中AlarmManager使用示例(持续更新)相关的知识,希望能为你提供帮助。
现在普遍的手机都会有一个闹钟的功能,如果使用android来实现一个闹钟可以使用AtarmManager来实现。AtarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服务。AlarmManager对象一般不直接实例化,而是通过Context.getsystemservice(Context.ALARM_SERVICE)方法获得。
【Android中AlarmManager使用示例(持续更新)】下面是我们运用之前用到的TimePickerDialog结合AlarmManager,实现一个可以设定任意时间而且可以重复的闹钟,演示效果如下(大概每晚23:48会有闹钟通知):

Android中AlarmManager使用示例(持续更新)

文章图片

layout中activity_main.xml布局文件代码:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout 3xmlns:android="http://schemas.android.com/apk/res/android" 4xmlns:tools="http://schemas.android.com/tools" 5android:id="@+id/activity_main" 6android:layout_width="match_parent" 7android:layout_height="match_parent" 8android:orientation="vertical" 9tools:context="com.example.administrator.alarmdemo.MainActivity"> 10< TextView 11android:id="@+id/time_tv" 12android:layout_width="match_parent" 13android:layout_height="wrap_content" 14android:hint="请设置闹铃时间" 15android:clickable="true" 16android:onClick="setTime" 17android:gravity="center"/> 18< TextView 19android:id="@+id/time_tv2" 20android:layout_width="match_parent" 21android:layout_height="wrap_content" 22android:hint="请设置响铃间隔时间" 23android:clickable="true" 24android:onClick="setIntervalTime" 25android:gravity="center"/> 26< Button 27android:layout_width="match_parent" 28android:layout_height="wrap_content" 29android:onClick="open" 30android:text="开启闹钟"/> 31< Button 32android:layout_width="match_parent" 33android:layout_height="wrap_content" 34android:onClick="stop" 35android:text="结束闹钟"/> 36 < /LinearLayout>

AndroidMainfest.xml配置文件(添加自定义重复闹铃receiver):
1 < ?xml version="1.0" encoding="utf-8"?> 2 < manifest xmlns:android="http://schemas.android.com/apk/res/android" 3package="com.example.administrator.alarmdemo"> 4< application 5android:allowBackup="true" 6android:icon="@mipmap/ic_launcher" 7android:label="@string/app_name" 8android:supportsRtl="true" 9android:theme="@style/AppTheme"> 10< activity android:name=".MainActivity"> 11< intent-filter> 12< action android:name="android.intent.action.MAIN" /> 13 14< category android:name="android.intent.category.LAUNCHER" /> 15< /intent-filter> 16< /activity> 17< receiver 18android:name=".RepeatAlarmReceiver" 19android:enabled="true" 20android:exported="true"> 21< intent-filter> 22< action android:name="repeatAlarm" /> 23< /intent-filter> 24< /receiver> 25< activity android:name=".Main2Activity"> < /activity> 26< /application> 27 28 < /manifest>

RepeatAlarmReceiver.java广播接收文件:
1 import android.content.BroadcastReceiver; 2 import android.content.Context; 3 import android.content.Intent; 4 import android.widget.Toast; 5 6 public class RepeatAlarmReceiver extends BroadcastReceiver { 7public RepeatAlarmReceiver() { 8} 9@Override 10public void onReceive(Context context, Intent intent) { 11Toast.makeText(context, "重复闹钟", Toast.LENGTH_SHORT).show(); 12} 13 }

MainActivity.java代码实现:
1 import android.app.AlarmManager; 2 import android.app.PendingIntent; 3 import android.app.TimePickerDialog; 4 import android.content.Intent; 5 import android.provider.Settings; 6 import android.support.v7.app.AppCompatActivity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.TextView; 10 import android.widget.TimePicker; 11 import java.util.Calendar; 12 public class MainActivity extends AppCompatActivity { 13AlarmManager alarmManagerSet; 14PendingIntent pendingIntentSet; 15int myHour,myMinute; //声明响铃时间 16int intervalHour,intervalMinute,intervalSecond; //声明间隔时间(响铃周期) 17Calendar calendar; //声明日历 18TextView time_tv,time_tv2; 19 20@Override 21protected void onCreate(Bundle savedInstanceState) { 22super.onCreate(savedInstanceState); 23setContentView(R.layout.activity_main); 24alarmManagerSet= (AlarmManager) getSystemService(ALARM_SERVICE); 25//设置声明时间格式 26calendar = Calendar.getInstance(); 27myHour=calendar.get(Calendar.HOUR_OF_DAY); 28myMinute=calendar.get(Calendar.MINUTE); 29intervalHour=calendar.get(Calendar.HOUR_OF_DAY); 30intervalMinute=calendar.get(Calendar.MINUTE); 31//获取时间设置的TextView 32time_tv= (TextView) findViewById(R.id.time_tv); 33time_tv2= (TextView) findViewById(R.id.time_tv2); 34} 35//使用timePickerDialog设置闹铃响铃时间 36public void setTime(View view){ 37TimePickerDialog timePickerDialog =new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { 38@Override 39public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 40myHour=hourOfDay; 41myMinute=minute; 42time_tv.setText(myHour+":"+myMinute); 43} 44},myHour,myMinute,true); 45timePickerDialog.show(); 46} 47//使用timePickerDialog设置闹铃响铃周期(间隔) 48public void setIntervalTime(View view){ 49TimePickerDialog timePickerDialog =new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { 50@Override 51public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 52intervalHour=hourOfDay; 53intervalMinute=minute; 54time_tv2.setText(intervalHour+":"+intervalMinute); 55} 56},intervalHour,intervalMinute,true); 57timePickerDialog.show(); 58} 59public void open(View view){ 60//分别获取闹铃响铃时间以及闹铃间隔时间(用冒号分割存储于数组中) 61String[] triggerTime=time_tv.getText().toString().split(":"); 62String[] intervalTime=time_tv2.getText().toString().split(":"); 63//通过Calendar分别获取对应的小时和分钟(需要将String转换为int) 64Calendar calendar1 = Calendar.getInstance(); 65calendar1.set(Calendar.HOUR_OF_DAY,Integer.parseInt(triggerTime[0])); 66calendar1.set(Calendar.MINUTE,Integer.parseInt(triggerTime[1])); 67//得到点击触发的毫秒值(即闹钟提醒时间) 68long triggerAtMillis= calendar1.getTimeInMillis(); 69//设定pendinngIntent接受自定义闹铃广播 70pendingIntentSet = PendingIntent.getBroadcast(this,0,new Intent("repeatAlarm"),0); 71//判断如果当前系统时间大于设置的闹铃时间,则在第二天开始启用该闹铃 72if(System.currentTimeMillis()> triggerAtMillis){ 73triggerAtMillis=triggerAtMillis+24*60*60*1000; 74} 75//使用.setRepeating设置重复闹铃 76alarmManagerSet.setRepeating(AlarmManager.RTC_WAKEUP,triggerAtMillis,intervalMillis,pendingIntentSet); 77} 78public void stop(View v){ 79alarmManagerSet.cancel(pendingIntentSet); 80} 81 }

 

    推荐阅读