我的代码如下
许可
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
来电者的活动代码是
Intent addNewContact = new Intent(Intent.ACTION_INSERT);
addNewContact.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(addNewContact, ADD_NEW_CONTACT); // ADD_NEW_CONTACT = 2 for my specific purpose
调用者活动的onActivityResult为
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("OnActivityResult Test ", "Request code : " + requestCode
+ " " + " ResultCode : " + resultCode);
switch(requestCode) {
case 2:
if (resultCode == Activity.RESULT_OK) {
// code to Update my list view
}
}
}
我的列表视图在模拟器和设备上获得更新(我使用samsung galuxy检查)也不是Droid-X,所以如果我使用Droid-X,结果不会反映在列表中。
当我阅读Droid-X的Log cat msg时,我看到resultCode始终为0(ZERO),即使我正在添加新的联系人。
答案 0 :(得分:0)
您可以延长来电者Activity
并明确地setResult
finish
时呼叫,可能吗?
答案 1 :(得分:0)
我知道Droid-X,motoblur的问题是(每个moto的网站)模糊联系人API是基于Android 1.x中的旧Contacts API,而不是新的2.x ContactsContract API。 HTC可能会这样做。
参考:
New contacts created using ContactsContract do not appear in Contacts app
在您的情况下,当您添加新联系人时,您没有将结果代码设置为-1。所以更好的方法是不要在onActivityResult
中执行任何任务(如果您在添加联系人时执行)。扩展将收到内容更改回调的ContentObserver课程,您可以完成任务。
参考: 1. How to implement an Android ContentObserver
这是一个示例
public class Test extends Activity {
private NewContentObserver contentObserver = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout._layout);
//do another task
//Adding listener when new contact will be added in device.
contentObserver = new NewContentObserver();
this.getApplicationContext().getContentResolver().registerContentObserver (ContactsContract.Contacts.CONTENT_URI, true, contentObserver);
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
// unregister the provider
this.getApplicationContext().getContentResolver().unregisterContentObserver(contentObserver);
}
//Get newest contact
private Uri getNewestContactUri() {
String[] projection = new String[] {ContactsContract.Contacts._ID};
String orderBy = ContactsContract.Contacts._ID + " DESC";
Cursor cursor = TagsActivity.this.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection, null, null, orderBy);
int idIdx = -1;
try {
idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
} catch (Exception e) {
e.printStackTrace();
idIdx = -1;
}
if (idIdx != -1) {
int id = -1;
if (cursor.moveToFirst()) {
id = cursor.getInt(idIdx);
}
if (id != -1) {
return Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,
Integer.toString(id));
}
}
return null;
}
private class NewContentObserver extends ContentObserver {
public NewContentObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Uri contactData = getNewestContactUri();
Cursor cursor = managedQuery(contactData, null, null, null, null);
if (cursor.moveToFirst()) {
long newId = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts._ID));
String newDisplayName = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.DISPLAY_NAME));
Log.i("Test", "New contact Added. ID of newly added contact is : " + newId + " Name is : " + newDisplayName);
runOnUiThread(addNewContactToList);
}
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
}
//Since we cant update our UI from a thread this Runnable takes care of that!
private Runnable addNewContactToList = new Runnable() {
public void run() {
//add logic to update your list view
}
};
}
希望这会有所帮助。
更新:通讯录2.x API适用于运行Gingerbread(Android 2.3)或更高版本的MOTOBLUR手机。我的Droid X正在运行Moto的新姜饼,我很高兴现在可以使用它。