Android中AlarmManager使用示例(持续更新,已经更改)

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

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

Android中AlarmManager使用示例(持续更新,已经更改)

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

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.alarmdemo.MainActivity">
<TextView
android:id="@+id/time_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请设置闹铃时间"
android:clickable="true"
android:onClick="setTime"
android:gravity="center"/>
<TextView
android:id="@+id/time_tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请设置响铃间隔时间"
android:clickable="true"
android:onClick="setIntervalTime"
android:gravity="center"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="open"
android:text="开启闹钟"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="结束闹钟"/>
</LinearLayout>

AndroidMainfest.xml配置文件(添加自定义重复闹铃receiver):

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.alarmdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".RepeatAlarmReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="repeatAlarm" />
</intent-filter>
</receiver>
<activity android:name=".Main2Activity"></activity>
</application> </manifest>

RepeatAlarmReceiver.java广播接收文件:

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class RepeatAlarmReceiver extends BroadcastReceiver {
public RepeatAlarmReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "重复闹钟", Toast.LENGTH_SHORT).show();
}
}

MainActivity.java代码实现:

 import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManagerSet;
PendingIntent pendingIntentSet;
int myHour,myMinute;//声明响铃时间
int intervalHour,intervalMinute,intervalSecond;//声明间隔时间(响铃周期)
Calendar calendar;//声明日历
TextView time_tv,time_tv2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManagerSet= (AlarmManager) getSystemService(ALARM_SERVICE);
//设置声明时间格式
calendar = Calendar.getInstance();
myHour=calendar.get(Calendar.HOUR_OF_DAY);
myMinute=calendar.get(Calendar.MINUTE);
intervalHour=calendar.get(Calendar.HOUR_OF_DAY);
intervalMinute=calendar.get(Calendar.MINUTE);
//获取时间设置的TextView
time_tv= (TextView) findViewById(R.id.time_tv);
time_tv2= (TextView) findViewById(R.id.time_tv2);
}
//使用timePickerDialog设置闹铃响铃时间
public void setTime(View view){
TimePickerDialog timePickerDialog =new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
myHour=hourOfDay;
myMinute=minute;
time_tv.setText(myHour+":"+myMinute);
}
},myHour,myMinute,true);
timePickerDialog.show();
}
//使用timePickerDialog设置闹铃响铃周期(间隔)
public void setIntervalTime(View view){
TimePickerDialog timePickerDialog =new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
intervalHour=hourOfDay;
intervalMinute=minute;
time_tv2.setText(intervalHour+":"+intervalMinute);
}
},intervalHour,intervalMinute,true);
timePickerDialog.show();
}
public void open(View view){
//分别获取闹铃响铃时间以及闹铃间隔时间(用冒号分割存储于数组中)
String[] triggerTime=time_tv.getText().toString().split(":");
String[] intervalTime=time_tv2.getText().toString().split(":");
//通过Calendar分别获取对应的小时和分钟(需要将String转换为int)
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR_OF_DAY,Integer.parseInt(triggerTime[0]));
calendar1.set(Calendar.MINUTE,Integer.parseInt(triggerTime[1]));
//得到点击触发的毫秒值(即闹钟提醒时间)
long triggerAtMillis= calendar1.getTimeInMillis();
//设定pendinngIntent接受自定义闹铃广播
pendingIntentSet = PendingIntent.getBroadcast(this,0,new Intent("repeatAlarm"),0);
//判断如果当前系统时间大于设置的闹铃时间,则在第二天开始启用该闹铃
if(System.currentTimeMillis()>triggerAtMillis){
triggerAtMillis=triggerAtMillis+24*60*60*1000;
}
//使用.setRepeating设置重复闹铃
alarmManagerSet.setRepeating(AlarmManager.RTC_WAKEUP,triggerAtMillis,intervalMillis,pendingIntentSet);
}
public void stop(View v){
alarmManagerSet.cancel(pendingIntentSet);
}
}
上一篇:替换linux系统文件etc下passwd文件的字段获取真正的root权限


下一篇:lucene基础