009_01访问系统短信数据库
时间:2021-07-01 10:21:17
帮助过:47人阅读
package com.example.visit_sms_db;
2
3 import android.app.Activity;
4 import android.content.ContentResolver;
5 import android.content.ContentValues;
6 import android.database.Cursor;
7 import android.net.Uri;
8 import android.os.Bundle;
9 import android.util.Log;
10 import android.view.Menu;
11 import android.view.MenuItem;
12 import android.view.View;
13
14 public class MainActivity
extends Activity {
15
16 @Override
17 protected void onCreate(Bundle savedInstanceState) {
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.activity_main);
20
21 }
22
23 //访问系统短信数据
24 public void getSms(View v){
25 ContentResolver cr =
getContentResolver();
26 Cursor c = cr.query(Uri.parse("content://sms"),
new String[]{"_id", "address", "type", "body"},
null,
null,
null);
27 while(c.moveToNext()){
28 int id = c.getInt(0
);
29 String number = c.getString(1
);
30 int type = c.getInt(2
);
31 String body =c.getString(3
);
32 Log.i("getSms", id+"," + number + "," + type + "," +
body);
33 }
34 }
35
36 public void addSms(View v){
37 ContentResolver cr =
getContentResolver();
38 ContentValues contentValues =
new ContentValues();
39 contentValues.put("_id", 5
);
40 contentValues.put("address", 95555+""
);
41 contentValues.put("body", "您的尾号为9527的招行银行卡收到王思聪转账1000000,00.请查收!"
);
42 cr.insert(Uri.parse("content://sms"
), contentValues);
43 }
44 }
MainActivity.java
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:paddingBottom="@dimen/activity_vertical_margin"
6 android:paddingLeft="@dimen/activity_horizontal_margin"
7 android:paddingRight="@dimen/activity_horizontal_margin"
8 android:paddingTop="@dimen/activity_vertical_margin"
9 tools:context="com.example.visit_sms_db.MainActivity"
10 android:orientation="vertical" >
11
12 <TextView
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:text="@string/hello_world" />
16 <Button
17 android:layout_width="wrap_content"
18 android:layout_height="wrap_content"
19 android:text="获取系统短信"
20 android:onClick="getSms"
21 />
22
23 <Button
24 android:layout_width="wrap_content"
25 android:layout_height="wrap_content"
26 android:text="插入系统短信"
27 android:onClick="addSms"
28 />
29 </LinearLayout>
activity_main.xml
009_01访问系统短信数据库
标签: