【问题标题】:Updating adapter from BroadcastReceiver从 BroadcastReceiver 更新适配器
【发布时间】:2014-01-17 23:12:35
【问题描述】:

我需要一些关于我正在编写的应用程序的帮助。

我有一个简单的同步,应该从服务器获取数据并将其更新/插入到数据库中。

通过使用 AlarmManager 设置为每天运行一次的 BroadcastReceiver 完成同步。这工作正常,我可以在应用程序运行/未运行时获取数据并将其更新/插入到数据库中,没关系。

我遇到的问题是应用程序打开时 - 我用于显示片段的适配器无法更新(或者我不知道如何更新)。我需要更新适配器,以便刷新 FragmentActivity 中的 Fragments。

欢迎任何帮助,到目前为止,我已经浪费了好几个小时并且无法弄清楚。谢谢!

【问题讨论】:

    标签: android android-fragments broadcastreceiver android-fragmentactivity fragmentpageradapter


    【解决方案1】:

    在您的适配器所在的片段或活动中,您可以在 onNewIntent 调用中侦听广播接收器的意图,并将新数据放入您的 arrayList 或您使用的任何容器中,然后只需在您的适配器上调用 notifyDataSetChanged。

    编辑:

    您需要做的就是在您的接收器中触发一个意图并通过意图将新数据传递到您的活动中。确保您的活动设置为单个实例,以便不会打开它的新实例。您可能还需要设置另一个标志,但我不记得了。

    【讨论】:

    • 但是我如何将意图从我的广播接收器发送到主要活动,请记住我的广播接收器不是内部类!
    • 我刚刚添加到我的答案中
    • 我用它来传递数据,它在 MainActivity 中被拾取,但问题是如果我在进程运行时退出应用程序(而不是关闭它),它将重新打开活动,我在这里还缺少什么吗?感谢你目前的帮助! Intent data = new Intent(context, MainActivity.class);data.putExtra("newSize", cardsArray.length());data.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(data);
    • 刚刚尝试添加两个标志:data.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); data.addFlags(Intent.FLAG_FROM_BACKGROUND); 它仍然会启动活动,如果我不将标志活动添加到新任务,则会出现异常。我还将 MainActivity 放在清单中的单个实例中。
    • 当您不在应用程序中时,您期望的行为是什么?
    【解决方案2】:

    我解决了我遇到的问题 - 广播接收器和计时器,如下所示:

    Calendar c = Calendar.getInstance(TimeZone.getDefault());
    Intent i = new Intent(this, CardBroadcastReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis() + 4000, 20000, pi);
    
    Timer timer = new Timer();
    
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                     if(isRunning)
                     {
                         Log.i("ZEUS_REFRESHING_DATA", "NOW");
                         loadCards();
                     }
                }
            });
        }
    }, 0, 10000);
    

    其中 loadCards() 方法执行以下操作:

    public void loadCards()
    {
        MyDbHandler db = new MyDbHandler(this);
    
        cards = db.getAllCards();
        this.cardCount = db.getCountCards();
    
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), this);
        if(this.cardCount > 0)
        {
            mSectionsPagerAdapter.setCount(this.cardCount);
            mSectionsPagerAdapter.notifyDataSetChanged();
    
        }
        // Set up the ViewPager with the sections adapter.
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }
    

    所以基本上只是重新加载适配器。

    所以现在我可以在应用程序在后台或未使用 BroadcastReceiver 运行时重新加载卡片,当应用程序处于活动状态或在前台时,我只需使用计时器重新加载数据!

    就是这样!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      相关资源
      最近更新 更多