Android -- 利用ContentProvider 讀取和寫入簡訊
阿新 • • 發佈:2019-01-28
1. 讀寫簡訊 示例程式碼
均需要先獲得讀寫簡訊的許可權
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
讀取簡訊程式碼
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); }