太原网站seo搜索优化,之梦与wordpress哪个好用,跨境电商最好的平台,中铝国际山东建设有限公司网站前言
有什么办法可以在不打开App的时候#xff0c;也能够触发一些操作呢#xff1f;比如说发送通知#xff0c;解决这个需求的办法有很多种选择#xff0c;比如说官方推荐的WorkManager API#xff0c;可以在后台执行一次性、耗时、定时的任务#xff0c;但WorkManager是…前言
有什么办法可以在不打开App的时候也能够触发一些操作呢比如说发送通知解决这个需求的办法有很多种选择比如说官方推荐的WorkManager API可以在后台执行一次性、耗时、定时的任务但WorkManager是严格遵循电池优化策略的也就是并不精准虽然你可以设置为加急任务但也还是不能满足精准时间。
所以想要在精准时间触发通知就只能使用Alarm了。
前置准备
理清自己需要的闹钟类型首先选择闹钟类型
“经过的时间”闹钟
从设定闹钟开始计时经过特定的时间触发的闹钟与时区、语言无关
实时闹钟
基于世界协调时间UTC一般情况下按照现实时间触发的闹钟但该方法会受到用户改变系统时间时受到影响。
是否唤醒CPU
选择完闹钟类型后还需确定闹钟是否能够唤醒设备正常情况下关闭屏幕后一段时间CPU就会陷入“睡眠状态”非唤醒闹钟会等待CPU“醒来”的时候才一起触发唤醒闹钟则会直接唤醒CPU直接触发。
实现定时不重复闹钟
我们先来测试一个定时能够唤醒CPU仅此一次的闹钟来发送一条通知
如果target SDK为31以上且没有被加入电池优化策略白名单则还需要在manifest文件中添加精确闹钟权限该权限会在安装时授予。
如果target SDK为33以上发送通知需要通知权限该权限需要在发送通知前主动向系统请求不然发不了通知
!--通知权限--uses-permission android:nameandroid.permission.POST_NOTIFICATIONS /!--闹钟权限--uses-permission android:nameandroid.permission.SCHEDULE_EXACT_ALARM /
首先先创建一个广播接收器这个广播接收器用来执行闹钟时间到的时候我们需要执行的逻辑代码例如发送一条通知通知权限的请求本文不再书写默认视为你已获得通知权限本文使用的广播接收器是MyAlarmReceiver闹钟时间到的时候会发送一条通知标题是My notification内容为Hello World! 加一个随机数。
import android.Manifest;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;import java.util.Random;public class MyAlarmReceiver extends BroadcastReceiver {String CHANNEL_LOCATION_ID myAlarm;Overridepublic void onReceive(Context context, Intent intent) {Log.e(TAG, onReceive: NOTIFY_ALARM );int count new Random().nextInt(100);NotificationManager notificationManager ContextCompat.getSystemService(context, NotificationManager.class);if (Build.VERSION.SDK_INT Build.VERSION_CODES.O) {int importance NotificationManager.IMPORTANCE_DEFAULT;NotificationChannel channel new NotificationChannel(CHANNEL_LOCATION_ID, CHANNEL_LOCATION_ID, importance);channel.setDescription(test);notificationManager.createNotificationChannel(channel);}//通知的普通点按操作Intent intentN new Intent(context, MainActivity.class);PendingIntent pendingIntent PendingIntent.getActivity(context, 202, intentN, PendingIntent.FLAG_IMMUTABLE);NotificationCompat.Builder builder new NotificationCompat.Builder(context,CHANNEL_LOCATION_ID).setSmallIcon(R.drawable.notification_icon_blue)//发送通知必须指定一个smallIcon背景需透明.setContentTitle(My notification).setContentText(Hello World! count).setPriority(NotificationCompat.PRIORITY_DEFAULT).setContentIntent(pendingIntent);//发送通知检查权限if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) ! PackageManager.PERMISSION_GRANTED) {return;}NotificationManagerCompat.from(context).notify(count, builder.build());}
}
当然别忘了在AndroidManifest.xml中注册我们新增的receiver
application...receiver android:name.MyAlarmReceiverandroid:exportedfalse/receiver
/application 想要设置一个闹钟就需要给系统的闹钟服务发送一个类似“预定”一样的意图下面这段代码我设置在17点20分0秒的闹钟时间到的时候系统的闹钟服务就会发送一条广播到我们的广播接收器MyAlarmReceiver根据接收到的广播进行对应的逻辑操作。 private AlarmManager alarmManager;private PendingIntent pendingIntent;private String packageName;...private void initAlarm(Context context){Intent intent new Intent(context,MyAlarmReceiver.class);pendingIntent PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);alarmManager (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);}private void setOneAlarm(){Calendar calendar Calendar.getInstance();calendar.set(Calendar.HOUR_OF_DAY,17);calendar.set(Calendar.MINUTE,20);calendar.set(Calendar.SECOND,0);Log.i(TAG, notify time: new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(calendar.getTime()));if (Build.VERSION.SDK_INT Build.VERSION_CODES.M) {Log.w(TAG, alarm: must );alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);}else{Log.w(TAG, alarm: normal );alarmManager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);}}
好了前置准备我们都做完了 只要触发setOneAlarm()方法就设定了一个闹钟系统的闹钟服务会在17点20分0秒发送一条广播触发MyAlarmReceiver类中的onReceive()方法就能发送一条通知了。
但是
你的手机如果是三星或谷歌的pixel以上方法就已经足够了。如果你的手机是国产定制化过的系统
例如小米的MIUI华为VIVOOPPO等手机的话我们还需要获取由定制系统接管的权限拿小米的MIUI举例这个权限叫做 自启动权限 没有这个权限的情况下不一定能触发这个闹钟大部分时间都无法触发
打开App的应用设置页面我们就能看到这个权限其他系统也基本同理不在应用信息中就在手机管家中 把这个自启动开关打开再去设定闹钟就能触发一个定时闹钟了。
实现重复闹钟且自动取消
重复闹钟的实现很简单只需要设定闹钟的时候使用setRepeating方法就能指定第一次闹钟的时间以及重复的间隔。但想要自动取消该怎么办呢
想要取消闹钟就需要调用闹钟服务的cancel()方法且传递一个一样的pendingIntent
其实实现方法很简单只需要再定一个取消的闹钟就行了。
给我们的Receiver区分一下不同的闹钟做什么事根据intent中的enable值来区分是发送通知还是取消闹钟。
为1的时候就发送通知为0的时候就取消闹钟。
import android.Manifest;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;import java.util.Random;public class MyAlarmReceiver extends BroadcastReceiver {public static final String NOTIFY_ALARM tdsss.myalarmnotify1.MyAlarmReceiver;public static final String CANCEL_ALARM tdsss.myalarmnotify1.cacelAlarm;String CHANNEL_LOCATION_ID myAlarm;Overridepublic void onReceive(Context context, Intent intent) {int isEnable intent.getIntExtra(enable,-1);Log.e(TAG, alarm onReceive: );switch (isEnable){case 1:Log.e(TAG, onReceive: NOTIFY_ALARM );int count new Random().nextInt(100);NotificationManager notificationManager ContextCompat.getSystemService(context, NotificationManager.class);if (Build.VERSION.SDK_INT Build.VERSION_CODES.O) {int importance NotificationManager.IMPORTANCE_DEFAULT;NotificationChannel channel new NotificationChannel(CHANNEL_LOCATION_ID, CHANNEL_LOCATION_ID, importance);channel.setDescription(test);notificationManager.createNotificationChannel(channel);}//通知的普通点按操作Intent intentN new Intent(context, MainActivity.class);PendingIntent pendingIntent PendingIntent.getActivity(context, 202, intentN, PendingIntent.FLAG_IMMUTABLE);NotificationCompat.Builder builder new NotificationCompat.Builder(context,CHANNEL_LOCATION_ID).setSmallIcon(R.drawable.notification_icon_blue)//发送通知必须指定一个smallIcon背景需透明.setContentTitle(My notification).setContentText(Hello World! count).setPriority(NotificationCompat.PRIORITY_DEFAULT).setContentIntent(pendingIntent);//发送通知检查权限if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) ! PackageManager.PERMISSION_GRANTED) {return;}NotificationManagerCompat.from(context).notify(count, builder.build());break;case 0:Log.e(TAG, onReceive: CANCEL_ALARM );Intent cancel new Intent(context,MyAlarmReceiver.class);cancel.setAction(MyAlarmReceiver.NOTIFY_ALARM);intent.putExtra(enable,1);PendingIntent cancelPendingIntent PendingIntent.getBroadcast(context,0,cancel,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);AlarmManager alarmManager (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);alarmManager.cancel(cancelPendingIntent);break;default:Log.e(TAG, onReceive: );break;}}
} private void setRepeatAlarmAndCancel(){alarmManager.cancel(pendingIntent);Calendar calendar Calendar.getInstance();calendar.set(Calendar.HOUR_OF_DAY,17);calendar.set(Calendar.MINUTE,30);calendar.set(Calendar.SECOND,0);Log.e(TAG, notify time: new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(calendar.getTime()));alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),1000*60*1,pendingIntent);//cancel alarmCalendar cancelTime Calendar.getInstance();cancelTime.set(Calendar.HOUR_OF_DAY,17);cancelTime.set(Calendar.MINUTE,35);cancelTime.set(Calendar.SECOND,0);Log.e(TAG, cancel time: new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(cancelTime.getTime()));Intent cancelIntent new Intent(getContext(),MyAlarmReceiver.class);cancelIntent.setAction(MyAlarmReceiver.CANCEL_ALARM);cancelIntent.putExtra(enable,0);PendingIntent cancelPendingIntent PendingIntent.getBroadcast(getContext(),2,cancelIntent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);if (Build.VERSION.SDK_INT Build.VERSION_CODES.M) {Log.e(TAG, alarm: must );alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,cancelTime.getTimeInMillis(),cancelPendingIntent);}else{Log.e(TAG, alarm: normal );alarmManager.setExact(AlarmManager.RTC_WAKEUP,cancelTime.getTimeInMillis(),cancelPendingIntent);}}
设定完以后系统就会在17点30分0秒时发送5条通知然后自动取消不再重复还有更多的扩展用法就自己摸索啦本文只是简单的使用一下~ 文章转载自: http://www.morning.dkfrd.cn.gov.cn.dkfrd.cn http://www.morning.lxmks.cn.gov.cn.lxmks.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.yybcx.cn.gov.cn.yybcx.cn http://www.morning.xprq.cn.gov.cn.xprq.cn http://www.morning.cfynn.cn.gov.cn.cfynn.cn http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.kgphc.cn.gov.cn.kgphc.cn http://www.morning.flfdm.cn.gov.cn.flfdm.cn http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.yrccw.cn.gov.cn.yrccw.cn http://www.morning.srbfp.cn.gov.cn.srbfp.cn http://www.morning.yfzld.cn.gov.cn.yfzld.cn http://www.morning.nsyzm.cn.gov.cn.nsyzm.cn http://www.morning.rbbzn.cn.gov.cn.rbbzn.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn http://www.morning.czgtt.cn.gov.cn.czgtt.cn http://www.morning.fqyxb.cn.gov.cn.fqyxb.cn http://www.morning.plpqf.cn.gov.cn.plpqf.cn http://www.morning.jpjxb.cn.gov.cn.jpjxb.cn http://www.morning.trbxt.cn.gov.cn.trbxt.cn http://www.morning.kjkml.cn.gov.cn.kjkml.cn http://www.morning.tfwsk.cn.gov.cn.tfwsk.cn http://www.morning.rfpxq.cn.gov.cn.rfpxq.cn http://www.morning.xxzjb.cn.gov.cn.xxzjb.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.rbxsk.cn.gov.cn.rbxsk.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.0dirty.cn.gov.cn.0dirty.cn http://www.morning.sxmbk.cn.gov.cn.sxmbk.cn http://www.morning.lwtfr.cn.gov.cn.lwtfr.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.jkzjs.cn.gov.cn.jkzjs.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.mlhfr.cn.gov.cn.mlhfr.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.fbxlj.cn.gov.cn.fbxlj.cn http://www.morning.snbry.cn.gov.cn.snbry.cn http://www.morning.rynqh.cn.gov.cn.rynqh.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.benqc.com.gov.cn.benqc.com http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.dnvhfh.cn.gov.cn.dnvhfh.cn http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn http://www.morning.kztpn.cn.gov.cn.kztpn.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.jlschmy.com.gov.cn.jlschmy.com http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.wsxly.cn.gov.cn.wsxly.cn http://www.morning.c7512.cn.gov.cn.c7512.cn http://www.morning.ygrkg.cn.gov.cn.ygrkg.cn http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn