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!
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!