由于对短信读写操作的api 被隐藏了 , 我们需要使用《Java反射机制的学习》一文中提到的反射的方法得到隐藏API 。
android写sim卡短信相关代码
/***
* 1) byte[] smsc : 短信服务中心的地址,个人认为在复制到SIM卡过程中可以为空。
* 2) byte[] pdu : 中文翻译是协议数据单元,这个参数最为重要,一会我们会做详细地解释说明。
* 3) int status : 短信存储在Icc卡上的状态,有4种状态,1是已读,3是未读,5是已发送,7是未发送。
* @param smsc
* @param pdu
* @param status
*/
public boolean writeSMStoIcc(byte[] smsc,byte[] pdu,int status){
// mKeyboardHelper = new ReflectionInternal(this,"android.telephony.SmsManager");
//调用类,声明类,mKeyboardView,mPasswordEntry,为需要传递的参数
// mKeyboardHelper.setInt("copyMessageToIcc", 0);
boolean flag = false;
SmsManager newSmsManager = SmsManager.getDefault();
try {
Class smsManagerClass = Class.forName("android.telephony.SmsManager");
Method localMethod = smsManagerClass.getMethod("copyMessageToIcc",new Class[]{byte[].class,byte[].class,Integer.TYPE});
Object[] arrayList = new Object[3];
arrayList[0] = smsc;
arrayList[1] = pdu;
arrayList[2] = status;
try {
flag = ((Boolean)localMethod.invoke(newSmsManager, arrayList)).booleanValue();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
Log.e("NoSuchMethodException","NoSuchMethodException :"+ e.getMessage());
e.printStackTrace();
} catch (ClassNotFoundException ex) {
// TODO Auto-generated catch block
Log.e("ClassNotFoundException","ClassNotFoundException :"+ ex.getMessage());
ex.printStackTrace();
}
return flag;
}
android 读sim卡短信
public ArrayList<SmsMessage> getSmsList(){
ArrayList<SmsMessage> list = new ArrayList<SmsMessage>();
SmsManager newSmsManager = SmsManager.getDefault();
try {
Class<?> smsManagerClass = Class.forName("android.telephony.SmsManager");
Method localMethod = smsManagerClass.getMethod("getAllMessagesFromIcc",null);
try {
list = (ArrayList<SmsMessage>)localMethod.invoke(newSmsManager, null);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
Log.e("NoSuchMethodException","NoSuchMethodException :"+ e.getMessage());
e.printStackTrace();
} catch (ClassNotFoundException ex) {
// TODO Auto-generated catch block
Log.e("ClassNotFoundException","ClassNotFoundException :"+ ex.getMessage());
ex.printStackTrace();
}
return list;
}
获取联系人列表
/***
* 获取sim卡上联系人列表
* @param uri
* @return
*/
public List<SimContacts> getSimContactsList(Activity act){
Cursor mCursor = null;
List<SimContacts> list = new ArrayList<SimContacts>();
try {
mCursor = act.getContentResolver().query(Uri.parse("content://icc/adn"), null, null, null, null);
if(mCursor!=null){
while (mCursor.moveToNext()) {
SimContacts simContacts = new SimContacts();
int nameFieldColumnIndex = mCursor.getColumnIndex("name");
if(nameFieldColumnIndex>=0){
simContacts.setName(mCursor.getString(nameFieldColumnIndex));
}
int numberFieldColumnIndex = mCursor.getColumnIndex("number");
if(numberFieldColumnIndex>=0){
simContacts.setNumber(mCursor.getString(numberFieldColumnIndex));
}
int emailsFieldColumnIndex = mCursor.getColumnIndex("emails");
if(emailsFieldColumnIndex>=0){
simContacts.setEmails(mCursor.getString(emailsFieldColumnIndex));
}
int idFieldColumnIndex = mCursor.getColumnIndex("_id");
if(idFieldColumnIndex>=0){
simContacts.set_id(mCursor.getString(idFieldColumnIndex));
}
list.add(simContacts);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(mCursor!=null){
mCursor.close();
}
}
return list;
}
根据id 删除对应记录
public void deleteSimContactById(Activity act,String id) {
if (id != null) {
Cursor cursor = null;
String whereCon = "_id=" + id + "";
try {
act.getContentResolver().delete(Uri.parse("content://icc/adn"), whereCon, null);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
if(cursor!=null){
cursor.close();
}
}
}
}
根据用户名和电话号码定位联系人并删除
public boolean deleteSimContactByNameAndPhone(Activity act,String name,String phone) {
Cursor cursor = null;
int flagInt = 0;
try {
String where = "tag='" + name + "'";
where += " AND number='" + phone + "'";
flagInt = act.getContentResolver().delete(Uri.parse("content://icc/adn"), where, null);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
if(cursor!=null){
cursor.close();
}
}
if(flagInt>0){
return true;
}else{
return false;
}
}
插入联系人
public void simInsertByList(Activity act, List<SimContacts> list) {
for(SimContacts simContacts:list){
ContentValues values = new ContentValues();
values.put("tag", simContacts.getName());
values.put("number",simContacts.getNumber());
Uri uri = null;
try {
uri = act.getContentResolver().insert(Uri.parse("content://icc/adn"), values);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(uri!=null){
uri.toString();
}
}
}