首页 > 代码库 > Android中AlarmManager使用示例(持续更新)

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

现在普遍的手机都会有一个闹钟的功能,如果使用Android来实现一个闹钟可以使用AtarmManager来实现。AtarmManager提供了一种系统级的提示服务,允许你安排在将来的某个时间执行一个服务。AlarmManager对象一般不直接实例化,而是通过Context.getsystemservice(Context.ALARM_SERVICE)方法获得。

下面是我们运用之前用到的TimePickerDialog结合AlarmManager,实现一个可以设定任意时间而且可以重复的闹钟,演示效果如下(大概每晚23:48会有闹钟通知):

技术分享

layout中activity_main.xml布局文件代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:id="@+id/activity_main"
 6     android:layout_width="match_parent"
 7     android:layout_height="match_parent"
 8     android:orientation="vertical"
 9     tools:context="com.example.administrator.alarmdemo.MainActivity">
10     <TextView
11         android:id="@+id/time_tv"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:hint="请设置闹铃时间"
15         android:clickable="true"
16         android:onClick="setTime"
17         android:gravity="center"/>
18     <TextView
19         android:id="@+id/time_tv2"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:hint="请设置响铃间隔时间"
23         android:clickable="true"
24         android:onClick="setIntervalTime"
25         android:gravity="center"/>
26     <Button
27         android:layout_width="match_parent"
28         android:layout_height="wrap_content"
29         android:onClick="open"
30         android:text="开启闹钟"/>
31     <Button
32         android:layout_width="match_parent"
33         android:layout_height="wrap_content"
34         android:onClick="stop"
35         android: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"
 3     package="com.example.administrator.alarmdemo">
 4     <application
 5         android:allowBackup="true"
 6         android:icon="@mipmap/ic_launcher"
 7         android:label="@string/app_name"
 8         android:supportsRtl="true"
 9         android: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
18             android:name=".RepeatAlarmReceiver"
19             android:enabled="true"
20             android: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 {
 7     public RepeatAlarmReceiver() {
 8     }
 9     @Override
10     public void onReceive(Context context, Intent intent) {
11         Toast.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 
12 import java.util.Calendar;
13 
14 public class MainActivity extends AppCompatActivity {
15     AlarmManager alarmManagerSet;
16     PendingIntent pendingIntentSet;
17     int myHour,myMinute;//设置响铃时间
18     int intervalHour,intervalMinute,intervalSecond;//设置间隔时间(响铃周期)
19     Calendar calendar;//声明日历
20     TextView time_tv,time_tv2;
21 
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_main);
26         alarmManagerSet= (AlarmManager) getSystemService(ALARM_SERVICE);
27         calendar = Calendar.getInstance();
28         myHour=calendar.get(Calendar.HOUR_OF_DAY);
29         myMinute=calendar.get(Calendar.MINUTE);
30 
31         intervalHour=calendar.get(Calendar.HOUR_OF_DAY);
32         intervalMinute=calendar.get(Calendar.MINUTE);
33 
34         time_tv= (TextView) findViewById(R.id.time_tv);
35         time_tv2= (TextView) findViewById(R.id.time_tv2);
36     }
37     public void setTime(View view){
38         TimePickerDialog timePickerDialog =new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
39             @Override
40             public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
41                 myHour=hourOfDay;
42                 myMinute=minute;
43                 time_tv.setText(myHour+":"+myMinute);
44             }
45         },myHour,myMinute,true);
46         timePickerDialog.show();
47     }
48     public void setIntervalTime(View view){
49         TimePickerDialog timePickerDialog =new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
50             @Override
51             public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
52                 intervalHour=hourOfDay;
53                 intervalMinute=minute;
54                 time_tv2.setText(intervalHour+":"+intervalMinute);
55             }
56         },intervalHour,intervalMinute,true);
57         timePickerDialog.show();
58     }
59     public void open(View view){
60         String[] triggerTime=time_tv.getText().toString().split(":");
61         String[] intervalTime=time_tv2.getText().toString().split(":");
62         long triggerAtMillis= Integer.parseInt(triggerTime[0])*1000*60*60+Integer.parseInt(triggerTime[1])*60*1000+System.currentTimeMillis();
63         long intervalMillis=Integer.parseInt(intervalTime[0])*1000*60*60+Integer.parseInt(intervalTime[1])*60*1000;
64         pendingIntentSet = PendingIntent.getBroadcast(this,0,new Intent("repeatAlarm"),0);
65         alarmManagerSet.setRepeating(AlarmManager.RTC_WAKEUP,triggerAtMillis,intervalMillis,pendingIntentSet);
66     }
67     public void stop(View v){
68         alarmManagerSet.cancel(pendingIntentSet);
69     }
70 }

 

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