问题描述
我下面的回答添加联系人选择器我的应用程序。
问题是,的onActivityResult
是立即的与正确的 REQ code调用
( PICK_CONTACT
),但与结果$ C $的ç
0
和空
数据。
这是的不的再次调用,在用户实际选取一个联系人。
该AndroidManifest给出了这样的活动的android:launchMode =singleInstance>
因为我只希望这是一个实例
我做了什么错?
MainActivity.java:
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
...
addContactButton =(按钮)findViewById(R.id.addContactButton);
addContactButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
意向意图=新意图(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(意向,PICK_CONTACT);
}
});
}@覆盖
公共无效的onActivityResult(INT REQ code,INT结果code,意图数据){
Log.d(TAG的onActivityResult);
super.onActivityResult(REQ code,结果code,数据); 开关(REQ code){
案例(PICK_CONTACT):
如果(结果code == Activity.RESULT_OK){
乌里联络资料= data.getData();
光标C = getContentResolver()查询(联络资料,NULL,NULL,NULL,NULL);
如果(c.moveToFirst()){
字符串名称= c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//不管你要对选定联系人姓名做TODO。
Log.d(TAG,你选择了+名字+。);
}
}
打破;
}
}
该singleInstance立即触发回调。你有更多的相关信息link
I'm following the answer http://stackoverflow.com/a/867828/129805 to add a contact picker to my app.
The problem is that onActivityResult
is immediately invoked with the correct reqCode
(PICK_CONTACT
), but with aresultCode
of 0
and a null
for data.
It is not invoked again, when the user actually picks a contact.
The AndroidManifest gives this activity android:launchMode="singleInstance">
as I only ever want there to be one instance.
What have I done wrong?
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
addContactButton = (Button) findViewById(R.id.addContactButton);
addContactButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult");
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
Log.d(TAG, "you chose " + name + ".");
}
}
break;
}
}
The singleInstance fires the callback immediately. You have more info in this link
这篇关于为什么选择了接触之前的onActivityResult叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!