简体   繁体   English

Android:如何使用SDK将联系人添加到SIM?

[英]Android: how to add a contact to the SIM using the SDK?

I am writing an application which writes contacts in the SIM card of an Android phone. 我正在编写一个应用程序,用于在Android手机的SIM卡中写入联系人。 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. 该应用程序具有权限android.permission.READ_CONTACTS和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? 任何想法为什么第三个插入(数据表中的第二个)失败,而前两个插入(RawContacts中的1和数据中的1)成功?

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

anddroid? anddroid? I didn't look at the rest, but might be worth deleting a 'd' 我没有看其余的,但可能值得删除'd'

I have replicated this one on my system,but the contact get erased as soon as the mobile restart.This means the contacts are saving in the temporary sim of mobile. 我已经在我的系统上复制了这个,但是一旦移动设备重新启动,联系人就会被删除。这意味着联系人正在保存在移动设备的临时SIM卡中。 Is this same happening on your side or i am missing something. 同样的事情发生在你这边或我错过了什么。 I am using 3G sim. 我正在使用3G SIM卡。

Regards 问候

Data.CONTACT_ID必须由Data.RAW_CONTACT_ID替换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM