android sim 卡短信读写

        由于对短信读写操作的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();
			}
		}
	}
	

相关下载https://download.csdn.net/download/qgy92320/8967551

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NeoChing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值