Last active
August 13, 2019 17:57
-
-
Save wada811/57b37d771a393ec76af3 to your computer and use it in GitHub Desktop.
IntentService + ResultReceiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AndroidManifest.xml | |
<service android:name="MyIntentService" /> | |
# IntentService + ResultReceiver | |
public class MyIntentService extends IntentService { | |
public static class MyResultReceiver extends ResultReceiver { | |
private OnMyResultListener mListener; | |
public interface OnMyResultListener { | |
public void onReceiveResult(int resultCode, Bundle resultData); | |
} | |
public MyResultReceiver(Handler handler) { | |
super(handler); | |
} | |
public MyResultReceiver setOnMyResultListener(OnMyResultListener listener){ | |
mListener = listener; | |
return this; | |
} | |
@Override | |
protected void onReceiveResult(int resultCode, Bundle resultData){ | |
if(mListener != null){ | |
mListener.onReceiveResult(resultCode, resultData); | |
} | |
} | |
} | |
public MyIntentService(String name) { | |
super(name); | |
} | |
public MyIntentService() { | |
super(MyIntentService.class.getSimpleName()); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent){ | |
ResultReceiver receiver = intent.getParcelableExtra(MyResultReceiver.class.getSimpleName()); | |
boolean result = Boolean.TRUE; | |
if(result){ | |
Bundle resultData = new Bundle(); | |
receiver.send(Activity.RESULT_OK, resultData); | |
}else{ | |
receiver.send(Activity.RESULT_CANCELED, null); | |
} | |
} | |
} | |
# 呼び出しと結果受け取り | |
Intent intent = new Intent(this, MyIntentService.class); | |
intent.setData(Uri.parse(mPicturePath)); | |
intent.putExtra(MyResultReceiver.class.getSimpleName(), new MyResultReceiver(new Handler()).setOnMyResultListener(new OnMyResultListener(){ | |
@Override | |
public void onReceiveResult(int resultCode, Bundle resultData){ | |
if(resultCode == RESULT_OK){ | |
}else{ | |
} | |
} | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment