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