调度WakefulIntentService几个报警 [英] Scheduling several alarms with WakefulIntentService

查看:159
本文介绍了调度WakefulIntentService几个报警的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图安排多个警报发布帖子到Twitter。我不知道使用的,因为我得到一个NullPointerException异常。

我有一个职位名单,我想张贴到Twitter,所以这是我做的。
在WakefulIntentService类的scheduleAlarms方法我有一个for循环遍历的职位列表和添加一个帖子对象到putExtra方法:

 公共静态无效scheduleAlarms(AlarmListener监听器,上下文ctxt,
        布尔力){
    共享preferences preFS = ctxt.getShared preferences(NAME,0);
    长lastAlarm = prefs.getLong(LAST_ALARM,0);    如果(lastAlarm == 0
            ||力
            || (System.currentTimeMillis的()> lastAlarm和放大器;&安培;系统
                    .currentTimeMillis() - lastAlarm> listener.getMaxAge())){
        AlarmManager经理=(AlarmManager)ctxt
                .getSystemService(Context.ALARM_SERVICE);
        对于(迭代器<邮政和GT;迭代器= AlarmActivity.posts.iterator();迭代器
                .hasNext()){
            帖子帖子=(POST)iterator.next();            意图I =新意图(ctxt,AlarmReceiver.class);
            i.putExtra(后,后);
            PI的PendingIntent = PendingIntent.getBroadcast(ctxt,
                    (INT)post.getId()中,i,0);
            listener.scheduleAlarms(MGR,PI,ctxt);
        }    }
}

然后在我的AppService服务doWakefulWork方法,我得到的Parcelable后的对象,但我得到一个NullPointerException异常:

  @覆盖
保护无效doWakefulWork(意向意图){
    Log.i(AppService服务,我还醒着,我清醒(打哈欠)!);
    后后= intent.getParcelableExtra(后);
    的System.out.println( - + post.getPost());
}

这是做什么,我想要实现的正确方法吗?

在此先感谢


解决方案

  

的NullPointerException异常指出intent.getParcelableExtra(后)为null。


我的猜测是,你的广播接收器沿着你的后不转发额外的服务作为其调用的一部分到 sendWakefulWork()


  

这是做什么,我想要实现的正确方法吗?


大概不会,因为你只会发一个帖子,忽略所有的休息,你甚至不会做,如果正确的用户关机或重新启动他们的设备。

使用的有一个的排定报警的有一个的PendingIntent 。由于您的文章需要在持久性数据存储,有 WakefulIntentService 加载员额从数据存储发送。消除来自的PendingIntent ,因为他们不再需要,不能轻易在断电或重启事件时进行复原。

所有演员

I am trying to schedule several alarms to publish posts to twitter. I am not sure of the usage, because i am getting a NullPointerException.

i have a list with posts and i would like to post them to twitter, so this is what i did. In the scheduleAlarms method of WakefulIntentService class i have a for loop iterating over the posts List and adding a post object to the putExtra method:

public static void scheduleAlarms(AlarmListener listener, Context ctxt,
        boolean force) {
    SharedPreferences prefs = ctxt.getSharedPreferences(NAME, 0);
    long lastAlarm = prefs.getLong(LAST_ALARM, 0);

    if (lastAlarm == 0
            || force
            || (System.currentTimeMillis() > lastAlarm && System
                    .currentTimeMillis() - lastAlarm > listener.getMaxAge())) {
        AlarmManager mgr = (AlarmManager) ctxt
                .getSystemService(Context.ALARM_SERVICE);
        for (Iterator<Post> iterator = AlarmActivity.posts.iterator(); iterator
                .hasNext();) {
            Post post = (Post) iterator.next();

            Intent i = new Intent(ctxt, AlarmReceiver.class);
            i.putExtra("post", post);
            PendingIntent pi = PendingIntent.getBroadcast(ctxt,
                    (int) post.getId(), i, 0);
            listener.scheduleAlarms(mgr, pi, ctxt);
        }

    }
}

Then in my AppService doWakefulWork method i get the Parcelable post object but i am getting a nullpointerexception:

@Override
protected void doWakefulWork(Intent intent) {
    Log.i("AppService", "I'm awake! I'm awake! (yawn)");
    Post post = intent.getParcelableExtra("post");
    System.out.println("- " + post.getPost());
}

Is this the right way of doing what i want to achieve?

Thanks in advance

解决方案

the NullPointerException states that intent.getParcelableExtra("post") is null.

My guess is that your BroadcastReceiver is not forwarding along your post extra to the service as part of its call to sendWakefulWork().

Is this the right way of doing what i want to achieve?

Probably not, as you will only send one post, ignoring all the rest, and you will not even do that correctly if the user powers off or reboots their device.

Use one scheduled alarm with one PendingIntent. Since your posts need to be in a persistent data store, have the WakefulIntentService load the posts to send from the data store. Eliminate all extras from the PendingIntent, as they are no longer needed and cannot readily be reconstituted in case of a power-off or reboot event.

这篇关于调度WakefulIntentService几个报警的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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