From bac341992fa2c0cb9f04341ceeda424d7b78a0ea Mon Sep 17 00:00:00 2001 From: gumartinm Date: Sun, 24 Jun 2012 01:21:25 +0200 Subject: [PATCH] MobiAds: update view lists when downloading ad. Registering broadcastreceivers in my activities. --- .../src/de/android/mobiads/MobiAdsService.java | 4 +- .../src/de/android/mobiads/batch/MobiAdsBatch.java | 9 ++++- .../de/android/mobiads/list/MobiAdsLatestList.java | 44 +++++++++++++++++----- .../src/de/android/mobiads/list/MobiAdsList.java | 43 ++++++++++++++------- 4 files changed, 73 insertions(+), 27 deletions(-) diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsService.java b/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsService.java index 29cb231..116a6ad 100644 --- a/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsService.java +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/MobiAdsService.java @@ -72,8 +72,8 @@ public class MobiAdsService extends Service { @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. diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/batch/MobiAdsBatch.java b/Android/MobiAdsReloaded/src/de/android/mobiads/batch/MobiAdsBatch.java index 31ca84d..42d0ac1 100644 --- a/Android/MobiAdsReloaded/src/de/android/mobiads/batch/MobiAdsBatch.java +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/batch/MobiAdsBatch.java @@ -26,14 +26,13 @@ import org.json.JSONObject; 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 { @@ -119,6 +118,12 @@ 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) { diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatestList.java b/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatestList.java index 5d44ffe..c38ff02 100644 --- a/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatestList.java +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsLatestList.java @@ -8,9 +8,11 @@ import java.util.List; 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; @@ -30,15 +32,15 @@ import android.widget.ListView; import de.android.mobiads.R; import de.android.mobiads.provider.Indexer; -public class MobiAdsLatestList extends ListActivity implements LoaderManager.LoaderCallbacks> -{ +public class MobiAdsLatestList extends ListActivity implements LoaderManager.LoaderCallbacks> { 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); @@ -70,9 +72,31 @@ public class MobiAdsLatestList extends ListActivity implements LoaderManager.Loa 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> onCreateLoader(int id, Bundle args) { diff --git a/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsList.java b/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsList.java index 36e59bf..e747b83 100644 --- a/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsList.java +++ b/Android/MobiAdsReloaded/src/de/android/mobiads/list/MobiAdsList.java @@ -15,11 +15,13 @@ import android.app.FragmentManager; 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; @@ -43,12 +45,15 @@ import de.android.mobiads.R; 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); @@ -61,12 +66,34 @@ public class MobiAdsList extends Activity { // 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) { @@ -358,19 +385,9 @@ public class MobiAdsList extends Activity { // 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) { -- 2.1.4