使用Android的Intent调用另外一个activity的时候,采用的是多线程机制,异步方式。startActivityForResult之后被调用activity并没有马上返回结果给调用activity,Android的Acitivity对象中startActivityForResult的源代码中有相关的解释。

/**

* Launch an activity for which you would like a result when it finished.

* When this activity exits, your

* onActivityResult() method will be called with the given requestCode.

* Using a negative requestCode is the same as calling

* {@link #startActivity} (the activity is not launched as a sub-activity).

*

* <p>Note that this method should only be used with Intent protocols

* that are defined to return a result. In other protocols (such as

* {@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开发精要》有非常全面详细的介绍


更多相关文章

  1. [android] PhoneGap 在 android 下的实现原理
  2. android progressDialog的使用
  3. 基本Dalvik VM调用
  4. 进程博客纳入
  5. Android(安卓)kotlin上传头像实现
  6. 【Android】Android(安卓)Parcelable 源码解析
  7. 类加载机制系列2——深入理解Android中的类加载器
  8. Android中通过经纬度来过去到城市名称
  9. 从NDK在非Root手机上的调试原理探讨Android的安全机制

随机推荐

  1. android的服务进程Service的创建及启动
  2. Android(安卓)补间动画之平移动画Transla
  3. android使用主流库搭建应用框架
  4. 【定制Android系统】Android(安卓)7.1 实
  5. Unity 调用Android(安卓)arr包(互相调用并
  6. Android系列 adb操作命令详解
  7. Android开发者必备的技能你会吗?MVVM 最新
  8. 你真的懂Android(安卓)Handler吗?(一)
  9. android之测试
  10. 正则表达式的不包含