Registering broadcastreceivers in my activities.
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
- //This will be run in the main thread of this service. It might be interesting to use a Hanlder
- //for this receiver implemeting its own thread. :/
+ //This will be run in the main thread of this service. It might be interesting to use a Handler
+ //for this receiver implementing its own thread. :/
//TODO: If I do not want to have any trouble, to use a synchronize to access this code here and when
//receiving new ads. Besides you are using the same code xD. No time right now. I must improve my code
//but I am in a hurry.
import org.json.JSONTokener;
import android.content.ContentValues;
import android.content.Context;
+import android.content.Intent;
import android.database.Cursor;
import android.location.Location;
import android.net.Uri;
import android.net.http.AndroidHttpClient;
import android.util.Log;
import de.android.mobiads.MobiAdsService;
-import de.android.mobiads.R;
-import de.android.mobiads.list.MobiAdsLatestList;
import de.android.mobiads.provider.Indexer;
public class MobiAdsBatch {
if ((uriInsert = updatedIndexer(objects)) != null) {
try {
downloadAds((String)objects.get("image"), (String) objects.get("id"));
+
+ //Update view on activities.
+ Intent updateList = new Intent("de.android.mobiads.MOBIADSLISTRECEIVER");
+ MobiAdsBatch.this.context.sendBroadcast(updateList);
+
+ //Change notification (if there is one)
((MobiAdsService)MobiAdsBatch.this.context).updateNotification();
} catch (Throwable e1) {
import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.AsyncTaskLoader;
+import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.Bitmap;
import de.android.mobiads.R;
import de.android.mobiads.provider.Indexer;
-public class MobiAdsLatestList extends ListActivity implements LoaderManager.LoaderCallbacks<List<AdsEntry>>
-{
+public class MobiAdsLatestList extends ListActivity implements LoaderManager.LoaderCallbacks<List<AdsEntry>> {
AdsEntryLatestAdapter mAdapter;
-
+ private BroadcastReceiver mReceiver;
+
@Override
- public void onResume() {
- super.onResume();
-
- mAdapter = new AdsEntryLatestAdapter(this, R.layout.ads_entry_list_item);
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ mAdapter = new AdsEntryLatestAdapter(this, R.layout.ads_entry_list_item);
setListAdapter(mAdapter);
getListView().setTextFilterEnabled(true);
registerForContextMenu(getListView());
- //TODO: stop using onResume and to use a broadcast from the service about changes in database.
- getLoaderManager().restartLoader(0, null, this);
- }
+ getLoaderManager().initLoader(0, null, this);
+
+ mReceiver = new BroadcastReceiver() {
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ //This will be run in the main thread of this service. It might be interesting to use a Handler
+ //for this receiver implementing its own thread. :/
+ if(action.equals("de.android.mobiads.MOBIADSLISTRECEIVER")){
+ getLoaderManager().restartLoader(0, null, MobiAdsLatestList.this);
+ }
+ }
+ };
+
+ IntentFilter filter = new IntentFilter();
+ filter.addAction("de.android.mobiads.MOBIADSLISTRECEIVER");
+ registerReceiver(mReceiver, filter);
+ }
+
+ @Override
+ protected void onDestroy() {
+ unregisterReceiver(mReceiver);
+ super.onDestroy();
+ }
@Override
public Loader<List<AdsEntry>> onCreateLoader(int id, Bundle args) {
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.AsyncTaskLoader;
+import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
+import android.content.IntentFilter;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.Bitmap;
import de.android.mobiads.provider.Indexer;
public class MobiAdsList extends Activity {
+ private BroadcastReceiver mReceiver;
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar actionBar = getActionBar();
+ final MobiAdsListFragment list = new MobiAdsListFragment();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE);
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
- MobiAdsListFragment list = new MobiAdsListFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
+ mReceiver = new BroadcastReceiver() {
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ //This will be run in the main thread of this service. It might be interesting to use a Handler
+ //for this receiver implementing its own thread. :/
+ if(action.equals("de.android.mobiads.MOBIADSLISTRECEIVER")){
+ getLoaderManager().restartLoader(0, null, list);
+ }
+ }
+ };
+
+ IntentFilter filter = new IntentFilter();
+ filter.addAction("de.android.mobiads.MOBIADSLISTRECEIVER");
+ registerReceiver(mReceiver, filter);
+
}
+
+ @Override
+ protected void onDestroy() {
+ unregisterReceiver(mReceiver);
+ super.onDestroy();
+ }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Start out with a progress indicator.
setListShown(false);
- }
-
- //TODO: Broadcast receiver from service, and stop using onResume... :/
- @Override
- public void onResume() {
- super.onResume();
+ getLoaderManager().initLoader(0, null, this);
- // Prepare the loader. Either re-connect with an existing one,
- // or start a new one.
- //TODO: reload just if there are changes in the data base :/ What means: broadcast receiver from service...
- //getLoaderManager().initLoader(0, null, this);
- getLoaderManager().restartLoader(0, null, this);
- }
+ }
@Override
public void onListItemClick(ListView l, View v, int position, long id) {