当前位置: 代码迷 >> Android >> sim卡汉语言 android
  详细解决方案

sim卡汉语言 android

热度:48   发布时间:2016-05-01 18:55:05.0
sim卡中文 android
package com.launch;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Insert2SIMActivity extends Activity {
Button btnStart;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 通过控件的ID来实例化控件对象
btnStart = (Button) findViewById(R.id.start);

// 开始按钮触发事件
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getContact();
}
});
}

public void getContact() {
// 获得所有的联系人
Cursor cur = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
TextView view=(TextView)findViewById(R.id.info);
// 循环遍历
if (cur.moveToFirst()) {
int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);

int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
do {
// 获得联系人的ID号
String contactId = cur.getString(idColumn);
// 获得联系人姓名
String disPlayName = cur.getString(displayNameColumn);
// 查看该联系人有多少个电话号码。如果没有这返回值为0
int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (phoneCount > 0) {
// 获得联系人的电话号码
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = " + contactId, null, null);
if (phones.moveToFirst()) {
do {
// 遍历所有的电话号码
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//插入到SIM卡
insert2SIM(disPlayName,phoneNumber);

view.setText(disPlayName + ":" + phoneNumber);
System.out.println(disPlayName + ":" + phoneNumber);
} while (phones.moveToNext());
}

}

} while (cur.moveToNext());

view.setText("复制结束");

}else{
view.setText("获取不到联系人信息");
}
}

public void insert2SIM(String name,String number){
if(name==null||number==null||"".equalsIgnoreCase(name)||"".equalsIgnoreCase(number)){
return;
}
ContentValues values = new ContentValues();
values.put("tag", name);
values.put("number", encode(number));
Uri uri = Uri.parse("content://icc/adn");
getContentResolver().insert(uri, values);
}

public String encode(String name){
String retStr=name;
if (name.getBytes().length != name.length()) {//包含中文
byte[] adnString = null;
byte[] byteTag;
try {
byteTag = name.getBytes("utf-16-be");
System.arraycopy(byteTag,0,adnString,1,byteTag.length);
adnString = new byte[32];
adnString[0] = (byte)0x80;
retStr=new String(adnString);
} catch (UnsupportedEncodingException e) {
System.out.println("不支持该编码");
e.printStackTrace();
}

}
return retStr.trim();
}

 
  相关解决方案