intent intent = new intent(mainactivity.this, autoreceiver.class);
intent.setaction("video_timer");
// pendingintent这个类用于处理即将发生的事情
pendingintent sender = pendingintent.getbroadcast(mainactivity.this, 0, intent, pendingintent.flag_update_current);
alarmmanager am = (alarmmanager) getsystemservice(alarm_service);
log.e(tag, "onclick: 点击启动定时任务" );
// alarmmanager.elapsed_realtime_wakeup表示闹钟在睡眠状态下会唤醒系统并执行提示功能,该状态下闹钟使用相对时间
// systemclock.elapsedrealtime()表示手机开始到现在经过的时间
//intervalmillis 时间 (毫秒值) (10*1000==10秒)
am.setrepeating(alarmmanager.rtc_wakeup,
systemclock.elapsedrealtime(), 10* 1000, sender);
创建广播
public class autoreceiver extends broadcastreceiver {
private static final int notification_flag = 1;
private notificationmanager manager;//通知
context context= applictio.getcontext();
@suppresslint("newapi")
@override
public void onreceive(context context, intent intent) {
this.context=context;
manager = (notificationmanager) context.getsystemservice(context.notification_service);
if (build.version.sdk_int >= build.version_codes.o) {
string channelid = "chat";
string channelname = "聊天消息";
int importance = notificationmanager.importance_high;
createnotificationchannel(channelid, channelname, importance);
}
log.e("-----------", "onreceive: " intent.getaction());
if (intent.getaction().equals("video_timer")) {
intent resultintent = new intent(context, showactivity.class);
pendingintent pendingintent = pendingintent.getactivity(context, 0, resultintent, pendingintent.flag_update_current);
notification notification = new notificationcompat.builder(context, "chat")
.setautocancel(true)
.setcontenttitle("收到聊天消息")
.setcontenttext("今天晚上吃什么")
.setwhen(system.currenttimemillis())
.setsmallicon(r.mipmap.ic_launcher)
.setcontentintent(pendingintent)
.setlargeicon(bitmapfactory.decoderesource(context.getresources(),r.mipmap.ic_launcher))
.build();
manager.notify(1, notification);
// level16及之后增加的,api11可以使用getnotificatin()来替代
notification.flags |= notification.flag_auto_cancel; // flag_auto_cancel表明当通知被用户点击时,通知将被清除。
notification.defaults |= notification.default_vibrate;//使用默认的震动
// 在android进行通知处理,首先需要重系统哪里获得通知管理器notificationmanager,它是一个系统service。
notificationmanager manager = (notificationmanager) context
.getsystemservice(context.notification_service);
manager.notify(notification_flag, notification);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示
//关闭广播 这里最开始用的是取消注册好的广播,但是注销以后无法再次启动
// context.getpackagemanager().setcomponentenabledsetting( new componentname("广播的包名", autoreceiver.class.getname()),
// packagemanager.component_enabled_state_disabled,
// packagemanager.dont_kill_app);
pendingintent pendintent1 = pendingintent.getbroadcast(context, 0,
intent, 0);
// 与上面的intent匹配(filterequals(intent))的闹钟会被取消
// 进行闹铃取消
alarmmanager manager1 = (alarmmanager) context.getsystemservice(context.alarm_service);
manager1.cancel(pendintent1);
}
}
//通知消息
@targetapi(build.version_codes.o)
private void createnotificationchannel(string channelid, string channelname, int importance) {
notificationchannel channel = new notificationchannel(channelid, channelname, importance);
notificationmanager notificationmanager = (notificationmanager)context. getsystemservice(
context.notification_service);
notificationmanager.createnotificationchannel(channel);
}
}
在androidmanifest中静态注册
这就完成了,直接复制粘贴即可,如果有不理解的地方,私信即可