Question

I'm in need of some help with an app I am writing.

I have a simple synchronization that should get the data from the server and update/insert it in the database.

Synchronization is done through BroadcastReceiver that is set with AlarmManager to run once a day. This works fine, I can get the data and update/insert it in the database when the app is running/not running, doesn't matter.

The problem I have is when the app is open - the adapter I use for displaying my Fragments cannot be updated (or I don't have an idea how). I need to update the adapter so I can refresh the Fragments in the FragmentActivity.

Any help is welcome, I lost quite a few hours so far and can't figure it out. Thanks!

Was it helpful?

Solution 2

I fixed the issue I was having with this - a BroadcastReceiver and a Timer like so:

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);

Where the loadCards() method does the following:

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);
}

So basically just reloads the adapter.

So now I can reload the cards while the app is in the background or not running with the BroadcastReceiver and when the app is active or in foreground I just use the timer to reload the data!

And that's it!

OTHER TIPS

in your fragment or activity where your adapter is you can listen for the intent from the broadcast reciever in the onNewIntent call and put the new data into your arrayList or what ever container your using and then just call notifyDataSetChanged on your adapter.

EDIT:

All you need to do is in your reciever just fire off an intent and pass the new data via intent into your activity. Make sure your activity is set to be a single instance so that a new instance of it isnt opened up. You may need to set another flag as well but I cant remember off the top of my head.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top