AlarmManager实现闹钟
程序员文章站
2022-07-12 11:23:18
...
今天给大家介绍一下闹钟的实现,先看概念图:
其中氛围两种基础的,一为一次性闹钟,响一次就停止;二为周期闹钟,可以由自己设置周期时间,重复响铃,以下为代码:
xml部分:
activity_main.xml(设置闹钟的界面)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置闹钟(一次)"
android:onClick="setAlarmOnce"
></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置闹钟(周期)"
android:onClick="setAlarmRepeat"
></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消周期闹钟"
android:onClick="cancelAlarmRepeat"
></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送通知"
android:onClick="send"
/>
</LinearLayout>
activity_ring.xml(闹钟响铃的界面)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="懒PIG,该出窝了!!!"
android:textSize="40sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止"
android:onClick="stop"
/>
</LinearLayout>
java部分:
MainActivity.java:
package
com.zking.administrator.g160628_android29_alarm_notification;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private AlarmManager am;
private PendingIntent pendingIntent;
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取闹钟管理器
am = (AlarmManager) getSystemService
(Context.ALARM_SERVICE);
//获取通知管理器
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
}
public void setAlarmOnce(View view){
//获取当前系统时间
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
//1.弹出时间对话框
TimePickerDialog timePickerDialog=new
TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener
() {
@Override
public void onTimeSet(TimePicker view, int
hourOfDay, int minute) {
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,hourOfDay);
c.set(Calendar.MINUTE,minute);
//2.获取到时间 hourOfDay minute
//3.设置闹钟
PendingIntent
pendingIntent=PendingIntent.getBroadcast
(MainActivity.this,0x101,new Intent
("com.zking.administrator.g160628_android29_alarm_notification
.RING"),0);
am.set
(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
}
},hour,minute,true);
timePickerDialog.show();
}
public void setAlarmRepeat(View view){
//获取当前系统时间
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
//1.弹出时间对话框
TimePickerDialog timePickerDialog=new
TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener
() {
@Override
public void onTimeSet(TimePicker view, int
hourOfDay, int minute) {
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,hourOfDay);
c.set(Calendar.MINUTE,minute);
//2.获取时间
//3.设置闹钟
pendingIntent = PendingIntent.getBroadcast
(MainActivity.this,0x102,new Intent
("com.zking.administrator.g160628_android29_alarm_notification
.RING"),0);
am.setRepeating
(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000,
pendingIntent);
}
},hour,minute,true);
timePickerDialog.show();
}
public void cancelAlarmRepeat(View view){
am.cancel(pendingIntent);
}
public void send(View view){
//实例化通知
NotificationCompat.Builder builder=new
NotificationCompat.Builder(this);
builder.setContentTitle("提示");
builder.setContentText("中奖啦!请先汇款到XXX账号****");
builder.setSmallIcon(android.R.drawable.star_on);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setAutoCancel(true);
// builder.setContentIntent()
// builder.setCustomContentView()
Notification notification=builder.build();
//发送通知
notificationManager.notify(0x101,notification);
}
}
RingActivity.java
package com.zking.administrator.g160628_android29_alarm_notification;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class RingActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ring);
mediaPlayer = MediaPlayer.create(this, R.raw.c);
mediaPlayer.start();
}
public void stop(View view){
mediaPlayer.stop();
finish();
}
}
RingReceiver.java:
package com.zking.administrator.g160628_android29_alarm_notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by Administrator on 2017/7/23 0023.
*/
public class RingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if("com.zking.administrator.g160628_android29_alarm_notification.RING".equals(intent.getAction())){
Log.i("test","闹钟响了!!!");
//跳转到Activity中
Intent intent2=new Intent(context,RingActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
}
}
}
最后,不要忘记设置权限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zking.administrator.g160628_android29_alarm_notification">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<activity android:name=".RingActivity"></activity>
<receiver android:name=".RingReceiver"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="com.zking.administrator.g160628_android29_alarm_notification.RING"></action>
</intent-filter>
</receiver>
</application>
</manifest>
上一篇: Spring后置处理器