Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert AsyncTask to IntentService #10

Merged
merged 7 commits into from
Jun 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UltimateAndroidGradle/demoofui/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,9 @@
<action android:name="com.marshalchen.common.demoofui" />
</intent-filter>
</activity>

<service android:name=".pullMenuItem.ReloadIntentService" android:exported="false"/>
<service android:name=".switchbuttondemo.UseActivity$LongService" android:exported="false"/>
</application>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
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;

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);
}

@Override
public void onDestroy() {
super.onDestroy();
if (reloadReceiver != null)
LocalBroadcastManager.getInstance(this).unregisterReceiver(reloadReceiver);
}

@Override
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;
}

@Override
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");
Expand All @@ -44,4 +63,4 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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);
}
});

Expand All @@ -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);
Expand All @@ -78,34 +98,14 @@ private void findView() {
mListenerFinish.setVisibility(mListenerSb.isChecked() ? View.VISIBLE : View.INVISIBLE);
}

class LongTask extends AsyncTask<Void, Integer, Void> {

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;
}
Expand All @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions UltimateAndroidNormal/DemoOfUI/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@
<activity android:name=".blurdialogfragment.SampleActionBarActivity"></activity>
<activity android:name=".blurdialogfragment.SampleFullScreenActivity"></activity>
<activity android:name=".sampleModules.CustomPullToRefreshActivity" android:theme="@style/android:Theme.Holo.Light.DarkActionBar"></activity>

<service android:name=".jsoup.UtilsDemoActivity$TitleService" android:exported="false"/>
<service android:name=".sampleModules.DropDownListViewDemo$GetDataService" android:exported="false"/>
<service android:name=".staggeredgridview.StaggeredGridEmptyViewActivity$FetchDataService" android:exported="false"/>
<service android:name=".listviewfilter.ui.ListViewFilterActivity$PopulateService" android:exported="false"/>
</application>

<!--<uses-configuration android:reqTouchScreen="finger"/>-->
Expand Down
Loading