使用Android的Intent調用另一個activity的時候,採用的是多線程機制,異步方式。startActivityForResult以後被調用activity並無立刻返回結果給調用activity,Android的Acitivity對象中startActivityForResult的源代碼中有相關的解釋。android
/**多線程
* Launch an activity for which you would like a result異步
when it finished.* When this activity exits, yourthis
* onActivityResult() method will be called with the given requestCode.spa
* Using a negative requestCode is the same as calling.net
* {@link #startActivity} (the activity is not launched as a sub-activity).線程
*code
* <p>Note that this method should only be used with Intent protocols對象
* that are defined to return a result. In other protocols (such asci
* {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may
* not get the result when you expect. For example, if the activity you
* are launching uses the singleTask launch mode, it will not run in your
* task and thus you will immediately receive a cancel result.
*
* <p>As a special case, if you call startActivityForResult() with a requestCode
* >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your
* activity, then your window will not be displayed until a result is
* returned back from the started activity. This is to avoid visible
* flickering when redirecting to another activity.
*
* <p>This method throws {@link android.content.ActivityNotFoundException}
* if there was no Activity found to run the given Intent.
*
* @param intent The intent to start.
* @param requestCode If >= 0, this code will be returned in
* onActivityResult() when the activity exits.
* @param options Additional options for how the Activity should be started.
* See {@link android.content.Context#startActivity(Intent, Bundle)
* Context.startActivity(Intent, Bundle)} for more details.
*
* @throws android.content.ActivityNotFoundException
*
* @see #startActivity
*/
public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options)
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
Button pButton = (Button) findViewById(R.id.btn_return);
pButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent pIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//調用攝像頭action
startActivityForResult(pIntent,INTENT_CODE_IMAGE_CAPTURE);//requestcode
//startActivityForResult若是就立刻獲取intent對象的結果中不少成員是null
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==INTENT_CODE_IMAGE_CAPTURE && data != null) {
final ImageView pImageView =(ImageView)findViewById(R.id.imageview1);
Bundle pBundle = data.getExtras(); //從intent對象中獲取數據,
if (pBundle != null) {
Bitmap pBitmap = (Bitmap) pBundle.get("data");
if (pBitmap !=null) {
pImageView.setImageBitmap(pBitmap);
pImageView.refreshDrawableState();
Log.i("Result", "capture picture succeed");
}
else {
Log.i("Result", "capture picture failure");
}
}
}
else if (requestCode == 0) {
Toast.makeText(this, "te", Toast.LENGTH_LONG).show();
Log.i("other", "result");
}
}
關於Android的intent機制可參考:
http://www.oschina.net/question/565065_67909
《Android開發精要》有很是全面詳細的介紹