Android通知与AlarmManager,广播和服务

编程入门 行业动态 更新时间:2024-10-10 19:16:55
本文介绍了Android通知与AlarmManager,广播和服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的code为家政一个通知:

myActivity.java

公共类myActivity延伸活动{    保护无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);        的setContentView(R.layout.mylayout);        CAL = Calendar.getInstance();        //它被设置至10:30        cal.set(Calendar.HOUR,10);        cal.set(Calendar.MINUTE,30);        cal.set(Calendar.SECOND,0);        长启动= cal.getTimeInMillis();        如果(cal.before(Calendar.getInstance())){                 启动+ = AlarmManager.INTERVAL_FIFTEEN_MINUTES;        }        意图mainIntent =新意图(这一点,myReceiver.class);        pIntent = PendingIntent.getBroadcast(这一点,0,mainIntent,PendingIntent.FLAG_UPDATE_CURRENT);        AlarmManager myAlarm =(AlarmManager)getSystemService(ALARM_SERVICE);        myAlarm.setRepeating(AlarmManager.RTC_WAKEUP,启动,AlarmManager.INTERVAL_FIFTEEN_MINUTES,pIntent);    }}

myReceiver.java

公共类myReceiver扩展广播接收器{    @覆盖    公共无效的onReceive(上下文C,意图我){       意图myService1 =新意图(C,myAlarmService.class);       c.startService(myService1);    }}

myAlarmService.java

公共类myAlarmService延伸服务{@覆盖公众的IBinder onBind(意向为arg0){    返回null;}@覆盖公共无效的onCreate(){   super.onCreate();}@燮pressWarnings(德precation)@覆盖公共无效调用onStart(意向意图,诠释startId){    super.onStart(意向,startId);    displayNotification(); }@覆盖公共无效的onDestroy(){    super.onDestroy();}公共无效displayNotification(){     意图mainIntent =新意图(这一点,myActivity.class);     的PendingIntent pIntent = PendingIntent.getActivity(这一点,0,mainIntent,PendingIntent.FLAG_UPDATE_CURRENT);     NotificationManager纳米=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);     Notification.Builder建设者=新Notification.Builder(本);     builder.setContentIntent(pIntent)     .setAutoCancel(真)     .setSmallIcon(R.drawable.ic_noti)     .setTicker(的getString(R.string.notifmsg))     .setContentTitle(的getString(R.string.app_name))     .setContentText(的getString(R.string.notifmsg));     nm.notify(0,builder.build());}}

的Andr​​oidManifest.xml

<使用许可权的android:NAME =android.permission.WAKE_LOCK/>.........<服务机器人:名字=机器人myAlarmService:启用=真/><接收机器人:myReceiverNAME = />

如果时间还没有过去没有一切完美。通知出现时它必须出现。

但如果时间已经过去(我们假设它是上午10点31分)的通知触发每次...当我关闭并重新打开应用程序,当我点击的通知...它有一个非常奇怪的行为

我无法弄清楚什么是错的吧。你能不能帮我请(并解释原因,如果你找到一个解决方案),在此先感谢:)

解决方案

INT TEMP = calTemp.getTime()的compareTo(calendar.getTime());    如果(温度大于0){    }其他{        alarmManager.set(AlarmManager.RTC,calendar.getTimeInMillis(),                pendingIntent1);    }

在这里 calTemp 给当前的时间和日历给我想火报警的时间。因此,根据上述code。如果时间已过去那么的通知不会触发肯定的。

this is my code for menage a single notification:

myActivity.java

public class myActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); cal = Calendar.getInstance(); // it is set to 10.30 cal.set(Calendar.HOUR, 10); cal.set(Calendar.MINUTE, 30); cal.set(Calendar.SECOND, 0); long start = cal.getTimeInMillis(); if(cal.before(Calendar.getInstance())) { start += AlarmManager.INTERVAL_FIFTEEN_MINUTES; } Intent mainIntent = new Intent(this, myReceiver.class); pIntent = PendingIntent.getBroadcast(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager myAlarm = (AlarmManager)getSystemService(ALARM_SERVICE); myAlarm.setRepeating(AlarmManager.RTC_WAKEUP, start, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pIntent); } }

myReceiver.java

public class myReceiver extends BroadcastReceiver { @Override public void onReceive(Context c, Intent i) { Intent myService1 = new Intent(c, myAlarmService.class); c.startService(myService1); } }

myAlarmService.java

public class myAlarmService extends Service { @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); } @SuppressWarnings("deprecation") @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); displayNotification(); } @Override public void onDestroy() { super.onDestroy(); } public void displayNotification() { Intent mainIntent = new Intent(this, myActivity.class); PendingIntent pIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(this); builder.setContentIntent(pIntent) .setAutoCancel(true) .setSmallIcon(R.drawable.ic_noti) .setTicker(getString(R.string.notifmsg)) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.notifmsg)); nm.notify(0, builder.build()); } }

AndroidManifest.xml

<uses-permission android:name="android.permission.WAKE_LOCK" /> ... ... ... <service android:name=".myAlarmService" android:enabled="true" /> <receiver android:name=".myReceiver"/>

IF the time has NOT past yet everything works perfectly. The notification appears when it must appear.

BUT if the time HAS past (let's assume it is 10.31 AM) the notification fires every time... when I close and re-open the app, when I click on the notification... it has a really strange behavior.

I can't figure out what's wrong in it. Can you help me please (and explain why, if you find a solution), thanks in advance :)

解决方案

int temp = calTemp.getTime()pareTo(calendar.getTime()); if(temp > 0){ }else{ alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent1); }

here calTemp gives current time and calender gives the time i want to fire the alarm. So according to above code if the time has already past then the notification will not fire for sure .

更多推荐

Android通知与AlarmManager,广播和服务

本文发布于:2023-11-27 11:38:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:和服务   通知   Android   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!