2011-07-22 17 views
1

我的代码返回结果代码0(零)作为以下ACTION_INSERT总是德罗伊德-X

权限

<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 as

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 
        } 

     } 
    } 

我的列表视图获取更新仿真器和设备(我与三星galuxy检查)也Droid-X以外,所以如果我使用Droid-X的结果不反映在列表上。

当我读取Droid-X的Log cat msg时,我看到resultCode始终为0(ZERO),即使我添加了新的联系人。

回答

0

我知道Droid-X,motoblur的问题在于(每个moto的网站)模糊联系人API是基于Android 1.x中的旧Contacts API而不是新的2.x ContactsContract API 。 HTC可能也会这样做。

编号:

  1. Created contacts not showing up on HTC Evo

  2. 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的新姜饼,我很高兴现在可以运行。

0

您可以扩展来电Activity,并明确地呼叫setResult当它finish es,也许?

+0

@Sagar Hatekar我无法以这种方式做我的需要。你能解释一下吗? –