Sometimes, you might want to receive a result from the activity that you start. In that case, start the activity by callingstartActivityForResult()
(instead ofstartActivity()
). To then receive the result from the subsequent activity, implement theonActivityResult()
callback method. When the subsequent activity is done, it returns a result in anIntent
to youronActivityResult()
method.
这里实现的效果是,点击a Activity后,屏幕会跳转到手机的通讯录,任意选择点击一个通讯录中的联系人,则从通讯录Activity跳回到a Activity并在a Activity上显示你刚才选择通讯录联系人的名字。
效果图:
应用开始界面:
选择联系人后的应用开始界面,会发现textView接受到了联系人的名字并显示出来:
代码如下:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
_09StartActivityForResultActivity,这是一个入口Activity,这个demo只有这一个Activity,它也负责接受通讯录联系人的信息
public class _09StartActivityForResultActivity extends Activity {
public static final int PICK_CONTACT_RESULT = 120;
private TextView textView01;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.textView01 = (TextView) this.findViewById(R.id.textView01);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
pickContact();
}
return super.onTouchEvent(event);
}
/**
* 获取通讯录信息
*/
private void pickContact(){
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT_RESULT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_RESULT){
Cursor cursor = getContentResolver().query(data.getData(),
new String[]{Contacts.DISPLAY_NAME}, null, null, null);
if(cursor.moveToFirst()){
String name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
textView01.setText(name);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
This example shows the basic logic you should use in your onActivityResult()
method in order to handle anactivity result. The first condition checks whether the request was successful—if it was, thentheresultCode
will beRESULT_OK
—and whether the requestto which this result is responding is known—in this case, therequestCode
matches thesecond parameter sent withstartActivityForResult()
. From there, the code handles the activity result by querying thedata returned in anIntent
(thedata
parameter).
What happens is, a ContentResolver
performs a query against a content provider, which returns aCursor
that allows the queried data to be read. For more information, seethe Content Providers document.
总结:
start the activity by callingstartActivityForResult()
(instead ofstartActivity()
). To then receive the result from the subsequent activity, implement theonActivityResult()
callback method. When the subsequent activity is done, it returns a result in anIntent
to youronActivityResult()
method.
demo下载链接地址:http://download.csdn.net/detail/hello_haozi/4290897