1

I have a simple application where I am trying to learn how to:

1) Call a web service

2) Parse the Data

3) Save it to SQLiteDatabase

4) Set-up a ContentObserver/Broadcaster to detect changes in an Activity and change the UI

So far, I have done everything but having trouble with step 4.

Code:

LaunchActivity which shows the layout containing the UI which I eventually want to change launches the Service class:

Intent intent = new Intent(this, DataService.class);
intent.putExtra("restMethodType", 0);
startService(intent);

The DataService class then runs the desired AsyncTask Rest Method:

public class DataService extends IntentService {

    public DataService() {
        super("DataService");
    }

    public DataService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        RestMethodType restMethodType = RestMethodType.values()[intent.getIntExtra("restMethodType", 0)];

        switch (restMethodType) {
            case UPCOMING_MATCH:
                UpcomingMatchRestMethod restMethod = new UpcomingMatchRestMethod(getApplicationContext(), intent);
                restMethod.execute();
                break;
        }
    }
}

Within the onPostExecute method of the AsyncTask, I save the retrieved data in to the database and then broadcast the event:

long newRowId = database.insert(MatchContract.MatchEntry.TABLE_NAME, MatchContract.MatchEntry.COLUMN_NAME_LOCATION, values);

if(newRowId != -1){
    mIntent.putExtra("matchId", newRowId);
    LocalBroadcastManager.getInstance(mContext).sendBroadcast(mIntent);
}

Back in the LaunchActivity, I set-up a receiver object:

DataReceiver mDataReceiver;

@Override
protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter("What Goes In Here?");
    registerReceiver(mDataReceiver, intentFilter);
}

This is what the receiver looks like:

private class DataReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Do Stuff like update the UI
        // Make sure to wrap any View objects in runOnUIThread
    }
}

So what goes in the IntentFilter? And what else am I missing? I followed the Google Docs but getting a little confused.

Thank you in advance.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.