Android -- 利用ContentProvider 读取和写入短信

1. 读写短信 示例代码

均需要先获得读写短信的权限


    

读取短信代码

public void click(View view){
		//1.利用内容提供者  中间人 获取用户的短信数据.
		ContentResolver resolver  = getContentResolver();
		Uri uri = Uri.parse("content://sms/"); //根据分析 代表的是所有的短信的路径
		Cursor cursor = resolver.query(uri, new String[]{"address","date","body","type"}, null, null, null);
		StringBuffer sb = new StringBuffer();
		while(cursor.moveToNext()){
			String address =cursor.getString(0);
			String date  = cursor.getString(1);
			String body = cursor.getString(2);
			String type = cursor.getString(3);
			System.out.println(address+"--"+date+"---"+body+"---"+type);
			sb.append(address+"--"+date+"---"+body+"---"+type);
			sb.append("\n");
		}
		cursor.close();
		tv.setText(sb.toString());
	}

写短信代码

public void click(View view){
		ContentResolver resolver = getContentResolver();
		Uri uri = Uri.parse("content://sms/");
		ContentValues values = new ContentValues();
		values.put("address", "95533");
		values.put("type", "1");
		values.put("body", "公安局给您的建设银行赚了100,000,000.00");
		values.put("date",System.currentTimeMillis());
		resolver.insert(uri, values);		
	}




 

你可能感兴趣的:(Android)