AlarmManager不叫第二天如果应用程序休眠ABT1号天 [英] AlarmManager Not called next day if the app sleeps for abt 1 day

查看:201
本文介绍了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后,我认为它是非法的,直接做在你的广播接收器的对象。 <一href=\"http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29\"相对=nofollow>报价:


  

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


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

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

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

希望这有助于。

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

更新2:部分片段

 &LT;服务机器人。MyIntentServiceNAME =机器人:标签=@字符串/ my_intent_service_name/&GT;公共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号天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆