AlarmManager不叫第二天如果应用程序休眠ABT1号天

编程入门 行业动态 更新时间:2024-10-26 18:19:52
本文介绍了AlarmManager不叫第二天如果应用程序休眠ABT1号天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发Android应用程序,显示每12小时通知,如果时间被保存在数据库中。所以每次一个数据被输入或编辑在数据库中,我取消当前alarmmanager并使得我不漏掉一个开始新的新的。另外在重新启动我已经叫alarmmanager。在广播接收器,数据库检查入境,如发现通知设置和应用程序将会自动打开。

所以,当我通过手动更改日期测试应用程序,该应用程序的工作原理expected.Also在重新启动应用程序works.But如果我坚持了将近14个小时闲置的应用程序,该通知没有设置,但如果我打开应用程序,并暂停其通知设置后。

这是我如何调用alarmmanager。   意图alarmintent =新意图(背景下,package.Alarm_Manager.class);

alarmintent.putExtra(注意,通知);    发件人= PendingIntent.getBroadcast(上下文,0,alarmintent,PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA);    alarm_manger =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);    alarm_manger.cancel(寄件人);    日历CAL = Calendar.getInstance();    长今= cal.getTimeInMillis();    alarmintent =新意图(背景下,package.Alarm_Manager.class);    alarmintent.putExtra(注,通知);    发件人= PendingIntent.getBroadcast(上下文,0,alarmintent,PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA);    alarm_manger =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);    alarm_manger.setRepeating(AlarmManager.RTC_WAKEUP,现在AlarmManager.INTERVAL_HALF_DAY,发件人);

这是广播接收机

@覆盖公共无效的onReceive(上下文的背景下,意图意图){       NotificationManager马槽=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);       日历CAL = Calendar.getInstance();       日期=(int)的(cal.getTimeInMillis()/ 1000);       通知通知=新的通知(R.drawable.vlcsnap_396460,通知,System.currentTimeMillis的());       的PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,意图,0);       notification.setLatestEventInfo(背景下,应用,通知,contentIntent);       notification.flags = Notification.FLAG_INSISTENT;       manger.notify(0,通知);   }

解决方案

在看到你的Alarm_Manager code后,我认为它是非法的,直接做在你的广播接收器的对象。 报价:

  

如果这个广播接收器是通过一个标签发射,则对象从这个函数返回后,已无生命体征。

我相信没有其他的办法,而不是创建一个由你的广播接收器通知服务,并确保服务电话 setLatestEventInfo()与本身(这个)作为上下文。

为什么你的异步广播失败,同时您的应用程序运行时的工作原理是可能是提供给广播接收器上下文只生活在呼叫时您的应用程序不运行的BroadcastReceiver持续时间的原因。因此,通知服务,只有你的广播接收器已与临时上下文一起死亡运行后,缺少一个有效的上下文。

当你的应用程序运行时,可能广播自带您的活动或应用程序对象作为上下文,这仍然是vaild时通知管理器运行。

希望这有助于。

更新:的`IntentService``会做。你不想要一个全职服务为。

更新2:部分片段

<服务机器人。MyIntentServiceNAME =机器人:标签=@字符串/ my_intent_service_name/>公共final类MyIntentService扩展IntentService {    公共MyIntentService(){    超(服务名称);    //设置你需要的任何属性    }    @覆盖    公共无效的onCreate(){        super.onCreate();        //初始化做,例如,得到引用通知服务    }    @覆盖    保护无效onHandleIntent(意向意图){    //处理这个意图        // ...尤其是:        notification.setLatestEventInfo(这一点,应用,通知,contentIntent);        // ...    }}公共final类MyAlarmReceiver扩展广播接收器{    @覆盖    公共无效的onReceive(上下文的背景下,意图意图){        context.startService(新意图(上下文,MyIntentService.class));    }}

I am developing an android app which shows a notification every 12 hour if the time is saved in the database. So everytime a data is entered or edited in the database ,I cancel the current alarmmanager and start a fresh new one so that I dont miss one. Also on reboot I have called the alarmmanager. On the broadcast receiver, the database is checked for entry and if found a notification is set and the app is opened automatically.

So when I test the app by changing the date manually,the app works as expected.Also on reboot the app works.But if I keep the app idle for nearly 14 hours,the notification is not set ,but if I open the app and suspend it the notification is set after that.

This is how I call the alarmmanager. Intent alarmintent = new Intent(context, package.Alarm_Manager.class);

alarmintent.putExtra("note","Notify"); sender = PendingIntent.getBroadcast(context , 0 , alarmintent , PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA); alarm_manger = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); alarm_manger.cancel(sender); Calendar cal = Calendar.getInstance(); long now = cal.getTimeInMillis(); alarmintent = new Intent(context, package.Alarm_Manager.class); alarmintent.putExtra("note","Notification"); sender = PendingIntent.getBroadcast(context , 0 , alarmintent , PendingIntent.FLAG_CANCEL_CURRENT | Intent.FILL_IN_DATA); alarm_manger = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarm_manger.setRepeating(AlarmManager.RTC_WAKEUP, now, AlarmManager.INTERVAL_HALF_DAY, sender);

This is the broadcast receiver

@Override public void onReceive(Context context, Intent intent) { NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); Calendar cal = Calendar.getInstance(); date = (int)(cal.getTimeInMillis()/1000); Notification notification = new Notification(R.drawable.vlcsnap_396460 , "Notify" , System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0); notification.setLatestEventInfo(context, "App", "Notify" , contentIntent); notification.flags = Notification.FLAG_INSISTENT; manger.notify( 0 , notification); }

解决方案

After having seen your Alarm_Manager code, I think it is illegal to do this in your BroadcastReceiver object directly. Quote:

If this BroadcastReceiver was launched through a tag, then the object is no longer alive after returning from this function.

I believe there is no other way than to create a Service which is informed by your BroadcastReceiver, and make sure that the Service calls setLatestEventInfo() with itself (this) as the Context.

The reason why your asynchronous Broadcast fails while it works when your app is running is probably that the Context provided to the BroadcastReceiver lives only for the duration of the call to the BroadcastReceiver when your app does not run. So the Notification service, which only runs after your BroadcastReceiver has died along with the temporary context, is missing a valid context.

When your app runs, the Broadcast probably comes with your Activity or Application object as Context, and this is still vaild when the Notification manager runs.

Hope this helps.

Update: An `IntentService`` will do. You don't want a full time Service for that.

Update 2: Some snippets.

<service android:name=".MyIntentService" android:label="@string/my_intent_service_name" /> public final class MyIntentService extends IntentService { public MyIntentService() { super("service name"); // set any properties you need } @Override public void onCreate() { super.onCreate(); // do init, e.g. get reference to notification service } @Override protected void onHandleIntent(Intent intent) { // handle the intent // ...especially: notification.setLatestEventInfo(this, "App", "Notify" , contentIntent); // ... } } public final class MyAlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { context.startService(new Intent(context, MyIntentService.class)); } }

更多推荐

AlarmManager不叫第二天如果应用程序休眠ABT1号天

本文发布于:2023-11-24 02:55:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第二天   不叫   应用程序   AlarmManager   号天

发布评论

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

>www.elefans.com

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