开启Activity并获取其返回值-短信发送器选择联系人 Activity返回数据,获取Activity返回数据 |
1、技术点 a) Activity.startActivityForResult(Intent intent, int requestCode) 开启一个activity,当你想要从该activity(在其finish)身上获取数据 //Launch an activity for which you would like a result when it finished. b) 为ListView每个item注册点击事件 setOnItemClickListener(OnItemClickListener listener) c) Activity.setResult(int resultCode, Intent data) 设置result,该数据会返回给该acticity的caller。 //Call this to set the result that your activity will return to its caller. //参数data: The data to propagate back to the originating activity. d) Activity.finish() 当前activity需要被close,然后数据会被返回给该activity的caller,通过caller的onActivityResult()方法接收。 //Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult(). e) onActivityResult(int requestCode, int resultCode, Intent data) //当前activity开启其他的activity返回的数据,会被调用 //参数1:请求码, //参数2:结果码 //参数3:返回回来的intent
d) requestCode应用:用来区分不同的请求 /**
* TODO 一、单击重新开启一个Activity选择联系人1
*/
public void selectContact(View view) {
Intent intent = new Intent(this, ContactsActivity.class);
this.startActivityForResult(intent, 1);// 参数2:请求码
}
/**
* TODO 一、单击重新开启一个Activity选择联系人2
*/
public void selectContact2(View view) {
Intent intent = new Intent(this, ContactsActivity.class);
this.startActivityForResult(intent, 2);// 参数2:请求码
} e) resultCode应用:结果码,可以设置不同的结果码返回,代表不同的意义。 例如:1代表男,2代表女,
setResult(1, data)
setResult(2, data)
|
核心代码: 1、选择联系人 /**
* TODO 一、单击重新开启一个Activity选择联系人1
*/
public void selectContact(View view) {
Intent intent = new Intent(this, ContactsActivity.class);
this.startActivityForResult(intent, 1);// 参数2:请求码
}
/**
* TODO 一、单击重新开启一个Activity选择联系人2
*/
public void selectContact2(View view) {
Intent intent = new Intent(this, ContactsActivity.class);
this.startActivityForResult(intent, 2);// 参数2:请求码
} 二、为listview的item注册点击事件 三、返回数据给原始的activity,setResult(0, data); 四、finish() // TODO 二、为listview的item注册点击事件
lV.setOnItemClickListener(new OnItemClickListener() {
/*
* parent The AdapterView where the click happened view 发生点击的ListView
* view 发生点击的ListView中的组件
* position 哪个位置被点击了
*/
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ContactInfo contactInfo = allContacts.get(position);
String phone = contactInfo.getPhone();
Intent data = new Intent();
data.putExtra("phone", phone);
// TODO 三,返回数据给原始的activity(调用它的activity)
setResult(0, data);// 参数一:resultCode:结果码,参数二:返回给原始调用的activity
// TODO finish() 点击完后,要将当前activity销毁掉,ActivityResult会返回给launcher当前的activity。
ContactsActivity.this.finish();
}
});
四、onActivityResult(int requestCode, int resultCode, Intent data) // TODO 四、当前activity开启其他的activity返回的数据,会被调用
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String phone = data.getStringExtra("phone");
if (phone != null) {
if (requestCode == 1) {
et_number.setText(phone);
} else if (requestCode == 2) {
et_number2.setText(phone);
}
}
}
|
结果: |
异常:当联系人数据过多,会报cursor异常 分析:这是由于cursor没有关闭导致 10-17 18:21:05.654: E/ContentResolver(8165): Cursor leak detected, query params: uri=content://com.android.contacts/data, projection=null, selection=raw_contact_id=?, selectionArgs=[Ljava.lang.String;@4293acc8, sortOrder=null |