From a847e2d55d1cedee3a4403325aaa55d87101a8af Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Fri, 29 May 2015 20:31:00 -0500 Subject: [PATCH 1/7] convert ReloadAsyncTask to IntentService --- .../demoofui/src/main/AndroidManifest.xml | 2 + .../ProgressMenuItemActivity.java | 21 ++++++++- .../pullMenuItem/ReloadAsyncTask.java | 38 ---------------- .../pullMenuItem/ReloadIntentService.java | 44 +++++++++++++++++++ 4 files changed, 66 insertions(+), 39 deletions(-) delete mode 100755 UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ReloadAsyncTask.java create mode 100755 UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ReloadIntentService.java diff --git a/UltimateAndroidGradle/demoofui/src/main/AndroidManifest.xml b/UltimateAndroidGradle/demoofui/src/main/AndroidManifest.xml index 418148b3..0d360604 100644 --- a/UltimateAndroidGradle/demoofui/src/main/AndroidManifest.xml +++ b/UltimateAndroidGradle/demoofui/src/main/AndroidManifest.xml @@ -1084,6 +1084,8 @@ + + diff --git a/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ProgressMenuItemActivity.java b/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ProgressMenuItemActivity.java index 4f23ab15..6c163d5b 100755 --- a/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ProgressMenuItemActivity.java +++ b/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ProgressMenuItemActivity.java @@ -1,6 +1,9 @@ package com.marshalchen.common.demoofui.pullMenuItem; +import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; +import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; @@ -8,18 +11,32 @@ import com.marshalchen.common.commonUtils.basicUtils.BasicUtils; import com.marshalchen.common.demoofui.HowToUseActivity; import com.marshalchen.common.demoofui.R; +import com.marshalchen.common.demoofui.pullMenuItem.ReloadIntentService; +import com.marshalchen.common.demoofui.pullMenuItem.ReloadIntentService.ReloadReceiver; import progress.menu.item.ProgressMenuItemHelper; import progress.menu.item.ProgressMenuItemSize; public class ProgressMenuItemActivity extends ActionBarActivity { + static final String RELOAD_FILTER = "ProgressMenuItemActivity_reloadReceiver"; private ProgressMenuItemHelper progressHelper; + private ReloadReceiver reloadReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progress_menu_item_activity_main); + reloadReceiver = new ReloadReceiver(progressHelper); + LocalBroadcastManager.getInstance(this) + .registerReceiver(reloadReceiver, new IntentFilter(RELOAD_FILTER)); + } + + @Override + public void onDestroy() { + super.onDestroy(); + if (reloadReceiver != null) + LocalBroadcastManager.getInstance(this).unregisterReceiver(reloadReceiver); } @Override @@ -33,7 +50,9 @@ public boolean onCreateOptionsMenu(Menu menu) { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_refresh: - new ReloadAsyncTask(progressHelper).execute(); + Intent reloadService = new Intent(this, ReloadIntentService.class); + reloadReceiver.startProgress(); + this.startService(reloadService); return true; case R.id.howToUse: BasicUtils.sendIntent(this, HowToUseActivity.class, "data", "ProgressMenuItemActivity"); diff --git a/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ReloadAsyncTask.java b/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ReloadAsyncTask.java deleted file mode 100755 index 9970848e..00000000 --- a/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ReloadAsyncTask.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.marshalchen.common.demoofui.pullMenuItem; - -import android.os.AsyncTask; -import android.util.Log; - -import progress.menu.item.ProgressMenuItemHelper; - -public class ReloadAsyncTask extends AsyncTask { - - private ProgressMenuItemHelper progressHelper; - - public ReloadAsyncTask(ProgressMenuItemHelper progressHelper) { - this.progressHelper = progressHelper; - } - - @Override - protected void onPreExecute() { - super.onPreExecute(); - progressHelper.startProgress(); - } - - @Override - protected Void doInBackground(Void... params) { - try { - Thread.sleep(3000); - } catch (final InterruptedException e) { - Log.d(getClass().getCanonicalName(), e.toString()); - } - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - progressHelper.stopProgress(); - } - -} \ No newline at end of file diff --git a/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ReloadIntentService.java b/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ReloadIntentService.java new file mode 100755 index 00000000..f717a0f1 --- /dev/null +++ b/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ReloadIntentService.java @@ -0,0 +1,44 @@ +package com.marshalchen.common.demoofui.pullMenuItem; + +import android.app.IntentService; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.support.v4.content.LocalBroadcastManager; +import android.util.Log; + +import progress.menu.item.ProgressMenuItemHelper; + +public class ReloadIntentService extends IntentService { + public ReloadIntentService() { + super("ReloadIntentService"); + } + + public void onHandleIntent(Intent intent) { + try { + Thread.sleep(3000); + } catch (final InterruptedException e) { + Log.d(getClass().getCanonicalName(), e.toString()); + } + Intent resultIntent = new Intent(ProgressMenuItemActivity.RELOAD_FILTER); + LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent); + } + + public static class ReloadReceiver extends BroadcastReceiver { + + private ProgressMenuItemHelper progressHelper; + + public ReloadReceiver(ProgressMenuItemHelper progressHelper) { + this.progressHelper = progressHelper; + } + + public void startProgress() { + progressHelper.startProgress(); + } + + @Override + public void onReceive(Context receiverContext, Intent receiverIntent) { + progressHelper.stopProgress(); + } + } +} \ No newline at end of file From 126727087d0ddecf744856f2c39b7d809af864cc Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Fri, 29 May 2015 20:55:37 -0500 Subject: [PATCH 2/7] Convert Title AsyncTask to IntentService --- .../DemoOfUI/AndroidManifest.xml | 2 + .../demoofui/jsoup/UtilsDemoActivity.java | 62 +++++++++++++------ 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml b/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml index bae1b8ba..3691d8b9 100644 --- a/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml +++ b/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml @@ -261,6 +261,8 @@ + + diff --git a/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/jsoup/UtilsDemoActivity.java b/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/jsoup/UtilsDemoActivity.java index 75ce5af6..e7c66713 100644 --- a/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/jsoup/UtilsDemoActivity.java +++ b/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/jsoup/UtilsDemoActivity.java @@ -1,8 +1,14 @@ package com.marshalchen.common.demoofui.jsoup; import android.app.Activity; +import android.app.IntentService; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.os.AsyncTask; import android.os.Bundle; +import android.support.v4.content.LocalBroadcastManager; import android.widget.TextView; import butterknife.ButterKnife; import butterknife.InjectView; @@ -17,30 +23,55 @@ * Created by cym on 14-7-19. */ public class UtilsDemoActivity extends Activity { + private static final String TITLE_FILTER = "UtilsDemoActivity_titleReceiver"; String url = "http://blog.marshalchen.com/"; @InjectView(R.id.jsoupTextView) TextView mJsoupTextView; + Title titleReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.jsoup_activity); ButterKnife.inject(this); - new Title().execute(); + titleReceiver = new Title(); + LocalBroadcastManager.getInstance(this).registerReceiver(titleReceiver, new IntentFilter(TITLE_FILTER)); + Intent title = new Intent(this, TitleService.class); + title.putExtra("url", url); + this.startService(title); mJsoupTextView.setText(UtilsDemo.TestJsoup()); } - // Title AsyncTask - private class Title extends AsyncTask { - String title; + @Override + public void onDestroy() { + super.onDestroy(); + if (titleReceiver != null) + LocalBroadcastManager.getInstance(this).unregisterReceiver(titleReceiver); + } + private class Title extends BroadcastReceiver { @Override - protected void onPreExecute() { - super.onPreExecute(); + public void onReceive(Context receiverContext, Intent receiverIntent) { + String title = receiverIntent.getStringExtra("title"); + // Set title into TextView + Logs.d("title---" + title); +// TextView txttitle = (TextView) findViewById(R.id.titletxt); +// txttitle.setText(title); + } + } - @Override - protected Void doInBackground(Void... params) { + // Title IntentService + public static class TitleService extends IntentService { + private String url; + private String title; + + public TitleService() { + super("TitleService"); + } + + public void onHandleIntent(Intent intent) { + this.url = intent.getStringExtra("url"); try { // Connect to the web site Document document = Jsoup.connect(url).get(); @@ -49,18 +80,9 @@ protected Void doInBackground(Void... params) { } catch (IOException e) { e.printStackTrace(); } - return null; - } - - @Override - protected void onPostExecute(Void result) { - // Set title into TextView - Logs.d("title---" + title); -// TextView txttitle = (TextView) findViewById(R.id.titletxt); -// txttitle.setText(title); - + Intent resultIntent = new Intent(TITLE_FILTER); + resultIntent.putExtra("title", title); + LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent); } } - - } From 417a10b120fbeb4e2eb1fe0092009bf3c002eeb3 Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Fri, 29 May 2015 21:30:30 -0500 Subject: [PATCH 3/7] convert GetDataTask to IntentService --- .../DemoOfUI/AndroidManifest.xml | 1 + .../sampleModules/DropDownListViewDemo.java | 65 ++++++++++++++----- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml b/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml index 3691d8b9..e8782560 100644 --- a/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml +++ b/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml @@ -263,6 +263,7 @@ + diff --git a/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/sampleModules/DropDownListViewDemo.java b/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/sampleModules/DropDownListViewDemo.java index a8c7f716..324f80ba 100644 --- a/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/sampleModules/DropDownListViewDemo.java +++ b/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/sampleModules/DropDownListViewDemo.java @@ -6,8 +6,14 @@ import java.util.LinkedList; import android.app.Activity; +import android.app.IntentService; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.os.AsyncTask; import android.os.Bundle; +import android.support.v4.content.LocalBroadcastManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; @@ -35,6 +41,9 @@ public class DropDownListViewDemo extends Activity { public static final int MORE_DATA_MAX_COUNT = 3; public int moreDataCount = 0; + private GetDataReceiver onDropDownReceiver; + private GetDataReceiver onClickReceiver; + @Override public void onCreate(Bundle savedInstanceState) { // super.onCreate(savedInstanceState, R.layout.drop_down_listview_demo); @@ -42,20 +51,30 @@ public void onCreate(Bundle savedInstanceState) { setContentView(R.layout.drop_down_listview_demo); listView = (DropDownListView) findViewById(R.id.list_view); // set drop down listener + onDropDownReceiver = new GetDataReceiver(true); + LocalBroadcastManager.getInstance(this).registerReceiver(onDropDownReceiver, + new IntentFilter("onDropDownReceiver")); listView.setOnDropDownListener(new DropDownListView.OnDropDownListener() { @Override public void onDropDown() { - new GetDataTask(true).execute(); + Intent getData = new Intent(DropDownListViewDemo.this, GetDataService.class); + getData.putExtra("FILTER", "onDropDownReceiver"); + DropDownListViewDemo.this.startService(getData); } }); // set on bottom listener + onClickReceiver = new GetDataReceiver(false); + LocalBroadcastManager.getInstance(this).registerReceiver(onClickReceiver, + new IntentFilter("onClickReceiver")); listView.setOnBottomListener(new OnClickListener() { @Override public void onClick(View v) { - new GetDataTask(false).execute(); + Intent getData = new Intent(DropDownListViewDemo.this, GetDataService.class); + getData.putExtra("FILTER", "onClickReceiver"); + DropDownListViewDemo.this.startService(getData); } }); listView.setOnItemClickListener(new OnItemClickListener() { @@ -73,26 +92,25 @@ public void onItemClick(AdapterView parent, View view, int position, long id) listView.setAdapter(adapter); } - private class GetDataTask extends AsyncTask { + @Override + public void onDestroy() { + super.onDestroy(); + if (onClickReceiver != null) + LocalBroadcastManager.getInstance(this).unregisterReceiver(onClickReceiver); + if (onDropDownReceiver != null) + LocalBroadcastManager.getInstance(this).unregisterReceiver(onDropDownReceiver); + } + + private class GetDataReceiver extends BroadcastReceiver { private boolean isDropDown; - public GetDataTask(boolean isDropDown) { + public GetDataReceiver(boolean isDropDown) { this.isDropDown = isDropDown; } @Override - protected String[] doInBackground(Void... params) { - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - ; - } - return mStrings; - } - - @Override - protected void onPostExecute(String[] result) { + public void onReceive(Context receiverContext, Intent receiverIntent) { if (isDropDown) { listItems.addFirst("Added after drop down"); @@ -113,8 +131,23 @@ protected void onPostExecute(String[] result) { // should call onBottomComplete function of DropDownListView at end of on bottom complete. listView.onBottomComplete(); } + } + } + + public static class GetDataService extends IntentService { + + public GetDataService() { + super("GetDataService"); + } - super.onPostExecute(result); + public void onHandleIntent(Intent intent) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + ; + } + Intent resultIntent = new Intent(intent.getStringExtra("FILTER")); + LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent); } } } From 69afa46d64ff96a08f9bcdff8b27a5ba09c522c7 Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Sat, 30 May 2015 10:52:14 -0500 Subject: [PATCH 4/7] convert LongTask to IntentService --- .../demoofui/src/main/AndroidManifest.xml | 1 + .../switchbuttondemo/UseActivity.java | 81 ++++++++++++------- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/UltimateAndroidGradle/demoofui/src/main/AndroidManifest.xml b/UltimateAndroidGradle/demoofui/src/main/AndroidManifest.xml index 0d360604..df81b47b 100644 --- a/UltimateAndroidGradle/demoofui/src/main/AndroidManifest.xml +++ b/UltimateAndroidGradle/demoofui/src/main/AndroidManifest.xml @@ -1086,6 +1086,7 @@ + diff --git a/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/switchbuttondemo/UseActivity.java b/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/switchbuttondemo/UseActivity.java index 0f420c73..96636f59 100755 --- a/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/switchbuttondemo/UseActivity.java +++ b/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/switchbuttondemo/UseActivity.java @@ -1,8 +1,13 @@ package com.marshalchen.common.demoofui.switchbuttondemo; import android.app.Activity; -import android.os.AsyncTask; +import android.app.IntentService; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; +import android.support.v4.content.LocalBroadcastManager; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; @@ -22,6 +27,7 @@ public class UseActivity extends Activity implements OnClickListener { private ProgressBar mPb; private Button mStartBt, mToggleAniBt, mToggleNotAniBt, mCheckedAniBt, mCheckNotAniBt; private TextView mListenerFinish; + private LongServiceReceiver longServiceReceiver; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,11 +47,18 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { }); // work with stuff takes long + longServiceReceiver = new LongServiceReceiver(); + IntentFilter recevierFilter = new IntentFilter(); + recevierFilter.addAction("LongServiceReceiver_ReturnIntent"); + recevierFilter.addAction("PROGRESS_UPDATE"); + LocalBroadcastManager.getInstance(this).registerReceiver(longServiceReceiver, recevierFilter); mStartBt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - new LongTask().execute(); + Intent longService = new Intent(UseActivity.this, LongService.class); + longServiceReceiver.onPreExecute(); + UseActivity.this.startService(longService); } }); @@ -58,6 +71,13 @@ public void onClick(View v) { mCheckNotAniBt.setOnClickListener(this); } + @Override + public void onDestroy() { + super.onDestroy(); + if (longServiceReceiver != null) + LocalBroadcastManager.getInstance(this).unregisterReceiver(longServiceReceiver); + } + private void findView() { mListenerSb = (SwitchButton) findViewById(R.id.sb_use_listener); mLongSb = (SwitchButton) findViewById(R.id.sb_use_long); @@ -78,34 +98,14 @@ private void findView() { mListenerFinish.setVisibility(mListenerSb.isChecked() ? View.VISIBLE : View.INVISIBLE); } - class LongTask extends AsyncTask { - - private int progress = 0; + class LongServiceReceiver extends BroadcastReceiver { - @Override - protected void onPreExecute() { - super.onPreExecute(); + public void onPreExecute() { mLongSb.setChecked(false); mStartBt.setEnabled(false); } - @Override - protected Void doInBackground(Void... params) { - while (progress < 100) { - progress++; - try { - Thread.sleep(20); - } catch (InterruptedException e) { - e.printStackTrace(); - } - publishProgress(progress); - } - return null; - } - - @Override - protected void onProgressUpdate(Integer... values) { - super.onProgressUpdate(values); + public void onProgressUpdate(Integer... values) { if (values == null || values.length == 0) { return; } @@ -114,14 +114,41 @@ protected void onProgressUpdate(Integer... values) { } @Override - protected void onPostExecute(Void result) { - super.onPostExecute(result); + public void onReceive(Context receiverContext, Intent receiverIntent) { + if (receiverIntent.getAction().equals("PROGRESS_UPDATE")) { + onProgressUpdate(receiverIntent.getIntExtra("progress", 0)); + return; + } mLongSb.slideToChecked(true); mStartBt.setEnabled(true); } } + public static class LongService extends IntentService { + private int progress = 0; + + public LongService() { + super("LongService"); + } + + public void onHandleIntent(Intent intent) { + while (progress < 100) { + progress++; + try { + Thread.sleep(20); + } catch (InterruptedException e) { + e.printStackTrace(); + } + Intent updateIntent = new Intent("PROGRESS_UPDATE"); + updateIntent.putExtra("progress", progress); + LocalBroadcastManager.getInstance(this).sendBroadcast(updateIntent); + } + Intent resultIntent = new Intent("LongServiceReceiver_ReturnIntent"); + LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent); + } + } + @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { From 7c16b0edb7a558e18e4577aa4e851360af415ca4 Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Sat, 30 May 2015 11:15:49 -0500 Subject: [PATCH 5/7] convert FetchData AsyncTask to IntentService --- .../DemoOfUI/AndroidManifest.xml | 1 + .../StaggeredGridEmptyViewActivity.java | 50 ++++++++++++++----- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml b/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml index e8782560..05422df3 100644 --- a/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml +++ b/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml @@ -264,6 +264,7 @@ + diff --git a/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/staggeredgridview/StaggeredGridEmptyViewActivity.java b/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/staggeredgridview/StaggeredGridEmptyViewActivity.java index 5ce28da5..2e52193d 100644 --- a/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/staggeredgridview/StaggeredGridEmptyViewActivity.java +++ b/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/staggeredgridview/StaggeredGridEmptyViewActivity.java @@ -1,9 +1,14 @@ package com.marshalchen.common.demoofui.staggeredgridview; import android.app.Activity; -import android.os.AsyncTask; +import android.app.IntentService; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; import android.os.SystemClock; +import android.support.v4.content.LocalBroadcastManager; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -22,11 +27,13 @@ public class StaggeredGridEmptyViewActivity extends Activity implements AbsListV public static final String SAVED_DATA_KEY = "SAVED_DATA"; private static final int FETCH_DATA_TASK_DURATION = 2000; + private static final String FETCH_DATA_FILTER = "StaggeredGridEmptyViewActivity_fetchDataReceiver"; private StaggeredGridView mGridView; private SampleAdapter mAdapter; private ArrayList mData; + private BroadcastReceiver fetchDataReceiver; @Override protected void onCreate(Bundle savedInstanceState) { @@ -65,9 +72,24 @@ protected void onCreate(Bundle savedInstanceState) { mGridView.setOnItemClickListener(this); + fetchDataReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context receiverContext, Intent receiverIntent) { + fillAdapter(); + } + }; + LocalBroadcastManager.getInstance(this) + .registerReceiver(fetchDataReceiver, new IntentFilter(FETCH_DATA_FILTER)); fetchData(); } + @Override + public void onDestroy() { + super.onDestroy(); + if (fetchDataReceiver != null) + LocalBroadcastManager.getInstance(this).unregisterReceiver(fetchDataReceiver); + } + private void fillAdapter() { for (String data : mData) { mAdapter.add(data); @@ -75,18 +97,8 @@ private void fillAdapter() { } private void fetchData() { - new AsyncTask() { - @Override - protected Void doInBackground(Void... params) { - SystemClock.sleep(FETCH_DATA_TASK_DURATION); - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - fillAdapter(); - } - }.execute(); + Intent fetchData = new Intent(this, FetchDataService.class); + this.startService(fetchData); } @Override @@ -112,4 +124,16 @@ protected void onSaveInstanceState(final Bundle outState) { super.onSaveInstanceState(outState); outState.putStringArrayList(SAVED_DATA_KEY, mData); } + + public static class FetchDataService extends IntentService { + public FetchDataService() { + super("FetchDataService"); + } + + public void onHandleIntent(Intent intent) { + SystemClock.sleep(FETCH_DATA_TASK_DURATION); + Intent resultIntent = new Intent(FETCH_DATA_FILTER); + LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent); + } + } } From 83403174a9a51eae32f877ee54101621f344ca6c Mon Sep 17 00:00:00 2001 From: Yu Lin Date: Sat, 30 May 2015 16:05:49 -0500 Subject: [PATCH 6/7] convert Populate AsyncTask to IntentService --- .../DemoOfUI/AndroidManifest.xml | 1 + .../ui/ListViewFilterActivity.java | 96 ++++++++++++++----- 2 files changed, 72 insertions(+), 25 deletions(-) diff --git a/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml b/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml index 05422df3..3cf82c43 100644 --- a/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml +++ b/UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml @@ -265,6 +265,7 @@ + diff --git a/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/listviewfilter/ui/ListViewFilterActivity.java b/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/listviewfilter/ui/ListViewFilterActivity.java index 74b80b12..31def308 100644 --- a/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/listviewfilter/ui/ListViewFilterActivity.java +++ b/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/listviewfilter/ui/ListViewFilterActivity.java @@ -7,8 +7,13 @@ import java.util.Locale; import android.app.Activity; -import android.os.AsyncTask; +import android.app.IntentService; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; +import android.support.v4.content.LocalBroadcastManager; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; @@ -24,6 +29,8 @@ // Activity that display customized list view and search box public class ListViewFilterActivity extends Activity { + private static final String POPULATE_FILTER = "ListViewFilterActivity_populateReceiver"; + // an array of countries to display in the list static final String[] ITEMS = new String[]{"张三", "李四", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", @@ -106,6 +113,8 @@ public class ListViewFilterActivity extends Activity { // empty view TextView mEmptyView; + PoplulateReceiver populateReceiver; + @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { @@ -119,6 +128,10 @@ protected void onCreate(Bundle savedInstanceState) { mListSectionPos = new ArrayList(); mListItems = new ArrayList(); + populateReceiver = new PoplulateReceiver(); + LocalBroadcastManager.getInstance(this) + .registerReceiver(populateReceiver, new IntentFilter(POPULATE_FILTER)); + // for handling configuration change if (savedInstanceState != null) { mListItems = savedInstanceState.getStringArrayList("mListItems"); @@ -135,10 +148,23 @@ protected void onCreate(Bundle savedInstanceState) { setIndexBarViewVisibility(constraint); } } else { - new Poplulate().execute(mItems); + Intent populate = new Intent(this, PopulateService.class); + populateReceiver.showLoading(); + populate.putExtra("mListSectionPos", mListSectionPos); + populate.putExtra("mListItems", mListItems); + populate.putExtra("mItems", mItems); + populate.putExtra("params", mItems); + this.startService(populate); } } + @Override + public void onDestroy() { + super.onDestroy(); + if (populateReceiver != null) + LocalBroadcastManager.getInstance(this).unregisterReceiver(populateReceiver); + } + private void setupViews() { setContentView(R.layout.list_filter_main_act); mSearchView = (EditText) findViewById(R.id.search_view); @@ -246,7 +272,13 @@ protected void publishResults(CharSequence constraint, FilterResults results) { ArrayList filtered = (ArrayList) results.values; setIndexBarViewVisibility(constraint.toString()); // sort array and extract sections in background Thread - new Poplulate().execute(filtered); + Intent populate = new Intent(ListViewFilterActivity.this, PopulateService.class); + populateReceiver.showLoading(); + populate.putExtra("mListSectionPos", mListSectionPos); + populate.putExtra("mListItems", mListItems); + populate.putExtra("mItems", mItems); + populate.putExtra("params", filtered); + ListViewFilterActivity.this.startService(populate); } } @@ -260,9 +292,7 @@ private void setIndexBarViewVisibility(String constraint) { } } - // sort array and extract sections in background Thread here we use - // AsyncTask - private class Poplulate extends AsyncTask, Void, Void> { + private class PoplulateReceiver extends BroadcastReceiver { private void showLoading(View contentView, View loadingView, View emptyView) { @@ -285,18 +315,44 @@ private void showEmptyText(View contentView, View loadingView, emptyView.setVisibility(View.VISIBLE); } - @Override - protected void onPreExecute() { + protected void showLoading() { // show loading indicator showLoading(mListView, mLoadingView, mEmptyView); - super.onPreExecute(); } @Override - protected Void doInBackground(ArrayList... params) { + public void onReceive(Context receiverContext, Intent receiverIntent) { + mListItems = receiverIntent.getStringArrayListExtra("mListItems"); + mListSectionPos = receiverIntent.getIntegerArrayListExtra("mListSectionPos"); + if (mListItems.size() <= 0) { + showEmptyText(mListView, mLoadingView, mEmptyView); + } else { + setListAdaptor(); + showContent(mListView, mLoadingView, mEmptyView); + } + } + } + + // sort array and extract sections in background Thread here we use + // IntentService + public static class PopulateService extends IntentService { + ArrayList mListSectionPos; + ArrayList mListItems; + ArrayList mItems; + + public PopulateService() { + super("PopulateService"); + } + + public void onHandleIntent(Intent intent) { + this.mListSectionPos = intent.getIntegerArrayListExtra("mListSectionPos"); + this.mListItems = intent.getStringArrayListExtra("mListItems"); + this.mItems = intent.getStringArrayListExtra("mItems"); + ArrayList params = intent.getStringArrayListExtra("params"); + mListItems.clear(); mListSectionPos.clear(); - ArrayList items = params[0]; + ArrayList items = params; if (mItems.size() > 0) { // NOT forget to sort array @@ -320,20 +376,10 @@ protected Void doInBackground(ArrayList... params) { i++; } } - return null; - } - - @Override - protected void onPostExecute(Void result) { - if (!isCancelled()) { - if (mListItems.size() <= 0) { - showEmptyText(mListView, mLoadingView, mEmptyView); - } else { - setListAdaptor(); - showContent(mListView, mLoadingView, mEmptyView); - } - } - super.onPostExecute(result); + Intent resultIntent = new Intent(POPULATE_FILTER); + resultIntent.putExtra("mListItems", mListItems); + resultIntent.putExtra("mListSectionPos", mListSectionPos); + LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent); } } From 0cb7d85bc3750fde7b64519b8596a6326f87149c Mon Sep 17 00:00:00 2001 From: yulin2 Date: Sun, 31 May 2015 11:57:39 -0500 Subject: [PATCH 7/7] move receiver to onCreateOptionsMenu --- .../demoofui/pullMenuItem/ProgressMenuItemActivity.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ProgressMenuItemActivity.java b/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ProgressMenuItemActivity.java index 6c163d5b..c75d171a 100755 --- a/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ProgressMenuItemActivity.java +++ b/UltimateAndroidGradle/demoofui/src/main/java/com/marshalchen/common/demoofui/pullMenuItem/ProgressMenuItemActivity.java @@ -27,9 +27,6 @@ public class ProgressMenuItemActivity extends ActionBarActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progress_menu_item_activity_main); - reloadReceiver = new ReloadReceiver(progressHelper); - LocalBroadcastManager.getInstance(this) - .registerReceiver(reloadReceiver, new IntentFilter(RELOAD_FILTER)); } @Override @@ -43,6 +40,9 @@ public void onDestroy() { public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.progress_menu_item_refresh_menu, menu); progressHelper = new ProgressMenuItemHelper(menu, R.id.action_refresh, ProgressMenuItemSize.LARGE); + reloadReceiver = new ReloadReceiver(progressHelper); + LocalBroadcastManager.getInstance(this) + .registerReceiver(reloadReceiver, new IntentFilter(RELOAD_FILTER)); return true; } @@ -63,4 +63,4 @@ public boolean onOptionsItemSelected(MenuItem item) { } } -} \ No newline at end of file +}