ACTION_INSERT始终的Droid-X返回结果code 0(零) [英] Action_INSERT always returns result code 0 (ZERO) on Droid-X

查看:135
本文介绍了ACTION_INSERT始终的Droid-X返回结果code 0(零)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code是如下

权限

<uses-permission android:name="android.permission.WRITE_CONTACTS" />

和呼叫者活动code是

And caller Activity code is

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为

And onActivityResult of caller Activity 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结果不会对名单反映。

My list view gets update on emulator and device (I checked with samsung galuxy) also other than Droid-X, so result doesn't reflect on list if I am using Droid-X.

当我读到登录的Droid-X的猫味精,我看到的结果code始终为0(零),即使我添加新的联系人。

When I read Log cat msg of Droid-X I saw resultCode is always 0 (ZERO), even if I am adding new contact.

推荐答案

我知道,与Droid的-X的问题,MOTOBLUR是(每moto的网站)模糊联系​​人API是基于了Android中发现旧联系人API 1.x中,而不是新的2.x的ContactsContract API。这有可能是HTC做同样的。

I know that the problem with Droid-X, motoblur is that (per moto's website) the blur contacts API is based off of the old Contacts API found in Android 1.x, and not the new 2.x ContactsContract API. It's possible that HTC does the same.

编号:


  1. 创建的联系人没有出现在宏达电Evo

<一个href=\"http://stackoverflow.com/questions/3336019/new-contacts-created-using-contactscontract-do-not-appear-in-contacts-app\">New使用ContactsContract创建的联系人没有出现在联系人应用程序

在你的情况,你没有得到结果code为-1,当你添加新的联系人。所以,更好的方法没有做任何任务的onActivityResult (如果你这样做时添加的联系人)。扩展 ContentObserver 类,将得到回调更改内容,而且你可以做你的任务。

In your case you didn't get result code as -1, when you are adding new contact. So better way don't do any task (if you doing when contact is added) in onActivityResult . Extend ContentObserver class that will receive call backs for changes to content, and you can do your task.

参考:
1. 如何实现一个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
        }
    };
}

希望这会有所帮助。

Hope this will help.

更新联系人2.x的API适用于运行姜饼(Android 2.3的)或更高版本的MOTOBLUR手机。我的Droid X的运行摩托新的姜饼,我很满意,这现在工作。

这篇关于ACTION_INSERT始终的Droid-X返回结果code 0(零)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆