Skip to content

Instantly share code, notes, and snippets.

@showsky
Created June 12, 2014 07:55
Show Gist options
  • Select an option

  • Save showsky/781d542c0b03ce46b23b to your computer and use it in GitHub Desktop.

Select an option

Save showsky/781d542c0b03ce46b23b to your computer and use it in GitHub Desktop.
Android IntentService < ---- > Activity status callback
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.resultrecevie"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.resultrecevie.MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.resultrecevie.APIService"/>
</application>
</manifest>
package com.example.resultrecevie;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
public class APIResultReceive extends ResultReceiver {
private final static String TAG = "APIResultReceive";
public final static int API_START = 1;
public final static int API_END = 2;
public final static int API_SUCCESS = 3;
public final static int API_FAIL = 4;
private Receive receive = null;
public interface Receive {
public void onReceiveResult(int resultCode, Bundle resultData);
}
public APIResultReceive(Handler handler) {
super(handler);
}
public void setReceive(Receive receive) {
this.receive = receive;
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (receive != null) {
receive.onReceiveResult(resultCode, resultData);
}
}
}
package com.example.resultrecevie;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.ResultReceiver;
import android.os.SystemClock;
import android.util.Log;
public class APIService extends IntentService {
private final static String TAG = "APIService";
public final static String EXTRA_COUNT = "extra_count";
public final static String EXTRA_RECEIVE = "extra_receive";
public APIService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
sendData(
intent.getIntExtra(EXTRA_COUNT, 0),
(ResultReceiver) intent.getParcelableExtra(EXTRA_RECEIVE)
);
}
private void sendData(int count, ResultReceiver resultReceive) {
resultReceive.send(APIResultReceive.API_START, null);
for (int i = 0; i < count; i++) {
Log.i(TAG, "i = " + i);
SystemClock.sleep(1000);
}
resultReceive.send(APIResultReceive.API_END, null);
}
public static void sendData(Context context, int count, APIResultReceive resultReceive) {
Intent intent = new Intent(context, APIService.class);
intent.putExtra(EXTRA_COUNT, count);
intent.putExtra(EXTRA_RECEIVE, resultReceive);
context.startService(intent);
}
}
package com.example.resultrecevie;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
public class MyActivity extends Activity implements APIResultReceive.Receive {
private final static String TAG = "MyActivity";
private APIResultReceive resultReceive = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
resultReceive = new APIResultReceive(new Handler());
resultReceive.setReceive(this);
// start send data
APIService.sendData(this, 10, resultReceive);
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestory");
resultReceive.setReceive(null);
}
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
switch (resultCode) {
case APIResultReceive.API_START:
Log.d(TAG, "APIResultReceive.API_START");
break;
case APIResultReceive.API_SUCCESS:
Log.d(TAG, "APIResultReceive.API_SUCCESS");
break;
case APIResultReceive.API_FAIL:
Log.d(TAG, "APIResultReceive.API_FAIL");
break;
case APIResultReceive.API_END:
Log.d(TAG, "APIResultReceive.API_END");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment