Android的:如何添加一个联系人使用SDK的SIM卡?联系人、Android、SIM、SDK

由网友(惜你若命)分享简介:我在写这写的联系人在Android手机SIM卡的应用程序。我卡在那里的电话号码加的地步:有没有明显的原因发生异常下面是code片段。进口android.app.Activity;进口android.content.ContentResolver;进口android.content.ContentUris;进口an...

我在写这写的联系人在Android手机SIM卡的应用程序。我卡在那里的电话号码加的地步:有没有明显的原因发生异常

下面是code片段。

 进口android.app.Activity;
进口android.content.ContentResolver;
进口android.content.ContentUris;
进口android.content.ContentValues​​;
进口android.provider.ContactsContract.RawContacts;
进口android.provider.ContactsContract.Data;
进口android.provider.ContactsContract.RawContactsEntity;
进口android.provider.ContactsContract.CommonDataKinds.Phone;
进口android.provider.ContactsContract.CommonDataKinds.StructuredName;
进口android.provider.ContactsContract.RawContacts.Entity;
进口android.database.Cursor;
进口android.net.Uri;
进口android.os.Bundle;
进口android.widget.TextView;
[...]
尝试{
            //添加一行到RawContacts表
     ContentValues​​值=新ContentValues​​();
     values​​.put(RawContacts.ACCOUNT_TYPEcom.anddroid.contacts.sim);
     values​​.put(RawContacts.ACCOUNT_NAME,SIM卡);
     乌里rawContactUri = getContentResolver()插入(RawContacts.CONTENT_URI,值)。

            //获取新添加行的ID
     长rawContactId = ContentUris.parseId(rawContactUri);

            //添加一个名行的数据表,将它链接到新的RawContact
            //与CONTACT_ID列
     values​​.clear();
     values​​.put(Data.RAW_CONTACT_ID,rawContactId);
     values​​.put(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE);
     values​​.put(StructuredName.DISPLAY_NAME,姓名);
     cr.insert(Data.CONTENT_URI,价值观);
            //此插入成功

            //添加一个手机行的数据表,将它链接到新的RawContact
            //与CONTACT_ID列
     values​​.clear();
     values​​.put(Data.CONTACT_ID,rawContactId);
     values​​.put(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE);
     values​​.put(Phone.NUMBER,12345678901);
     values​​.put(Phone.TYPE,Phone.TYPE_MOBILE);
     cr.insert(Data.CONTENT_URI,价值观);
            //此插入失败,一个NullPointerException异常
}
赶上(例外五){
    字符串XX = e.toString();
    的System.out.println(XX);
}
 

该应用程序有权限android.permission.READ_CONTACTS和android.permission.WRITE_CONTACTS。

手机显示的名字,但没有电话的联系人(顺便提一句,增加手机使用一个新的联系人正常的UI结果该联系人添加,用姓名和电话,并与名称只有站稳在旧的联系人)。

任何想法,为什么第三插入(第二在数据表中)失败,而2 previous的人(1 RawContacts和1个数据)成功吗?

解决方案

  values​​.put(RawContacts.ACCOUNT_TYPE,com.anddroid.contacts.sim);
 
手机如何添加联系人到SIM卡

anddroid?我没有看其他地方,但可能是值得删除D

I am writing an application which writes contacts in the SIM card of an Android phone. I am stuck at the point where the phone number is added: an exception occurs with no apparent reason.

Here is a snippet of code.

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContactsEntity;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.RawContacts.Entity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
[...]
try{
            // add a row to the RawContacts table
     ContentValues values = new ContentValues();
     values.put(RawContacts.ACCOUNT_TYPE, "com.anddroid.contacts.sim");
     values.put(RawContacts.ACCOUNT_NAME, "SIM");
     Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);

            // get the ID of the newly-added line
     long rawContactId = ContentUris.parseId(rawContactUri);

            // add a "name" line to the Data table, linking it to the new RawContact
            // with the CONTACT_ID column
     values.clear();
     values.put(Data.RAW_CONTACT_ID, rawContactId);
     values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
     values.put(StructuredName.DISPLAY_NAME, "Name");
     cr.insert(Data.CONTENT_URI, values);
            // this insert succeeds

            // add a "phone" line to the Data table, linking it to the new RawContact
            // with the CONTACT_ID column
     values.clear();
     values.put(Data.CONTACT_ID, rawContactId);
     values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
     values.put(Phone.NUMBER, "+12345678901");
     values.put(Phone.TYPE, Phone.TYPE_MOBILE);
     cr.insert(Data.CONTENT_URI, values);
            // this insert fails with a NullPointerException
}
catch(Exception e){
    String xx=e.toString();
    System.out.println(xx);
}

The application has permissions android.permission.READ_CONTACTS and android.permission.WRITE_CONTACTS.

The phone shows a contact with the name but no phone (incidentally, adding the phone to that contact using the normal UI results in a new contact being added, with name and phone, and the old contact with name only staying).

Any idea why the third insert (the second in the Data table) fails, while the 2 previous ones (1 in RawContacts and 1 in Data) succeed?

解决方案

values.put(RawContacts.ACCOUNT_TYPE, "com.anddroid.contacts.sim");

anddroid? I didn't look at the rest, but might be worth deleting a 'd'

阅读全文

相关推荐

最新文章