简单的读取收件箱信息
核心代码
private void getMsgs(){
Uri uri = Uri.parse("content://sms/");
ContentResolver resolver = getContentResolver();
//获取的是哪些列的信息
Cursor cursor = resolver.query(uri, new String[]{"address","date","type","body"}, null, null, null);
while(cursor.moveToNext())
{
String address = cursor.getString(0);
String date = cursor.getString(1);
String type = cursor.getString(2);
String body = cursor.getString(3);
System.out.println("地址:" + address);
System.out.println("时间:" + date);
System.out.println("类型:" + type);
System.out.println("内容:" + body);
System.out.println("======================");
}
cursor.close();
}
AndroidManifest.xml加入读取收件箱的权限
<uses-permission android:name="android.permission.READ_SMS"/>
简单的往收件箱里插入一条信息
核心代码
private void insertMsg() {
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://sms/");
ContentValues conValues = new ContentValues();
conValues.put("address", "123456789");
conValues.put("type", 1);
conValues.put("date", System.currentTimeMillis());
conValues.put("body", "no zuo no die why you try!");
resolver.insert(uri, conValues);
Log.e("HeHe", "短信插入完毕~");
}
注意事项:
上述代码在4.4以下都可以实现写入短信的功能,而5.0上就无法写入,原因是: 从5.0开始,默认短信应用外的软件不能以写入短信数据库的形式发短信!