Android_10
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical" 8 tools:context=".MainActivity" > 9 10 <Button 11 android:id="@+id/button" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:text="读取" /> 15 16 <Button 17 android:id="@+id/button2" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" 20 android:text="插入" /> 21 </LinearLayout>
1 package com.example.myapplication2; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.ContentResolver; 6 import android.content.ContentValues; 7 import android.database.Cursor; 8 import android.net.Uri; 9 import android.os.Bundle; 10 import android.util.Log; 11 import android.view.View; 12 import android.widget.Button; 13 14 public class MainActivity extends AppCompatActivity { 15 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 21 Button button1 = (Button)findViewById(R.id.button); 22 Button button2 = (Button)findViewById(R.id.button2); 23 24 25 button1.setOnClickListener(new View.OnClickListener() { 26 @Override 27 public void onClick(View v) { 28 getMsgs(); 29 } 30 }); 31 32 button2.setOnClickListener(new View.OnClickListener() { 33 @Override 34 public void onClick(View v) { 35 insertMsg(); 36 } 37 }); 38 39 40 } 41 42 private void getMsgs(){ 43 Uri uri = Uri.parse("content://sms/"); 44 ContentResolver resolver = getContentResolver(); 45 //获取的是哪些列的信息 46 Cursor cursor = resolver.query(uri, new String[]{"address","date","type","body"}, null, null, null); 47 while(cursor.moveToNext()) 48 { 49 String address = cursor.getString(0); 50 String date = cursor.getString(1); 51 String type = cursor.getString(2); 52 String body = cursor.getString(3); 53 System.out.println("地址:" + address); 54 System.out.println("时间:" + date); 55 System.out.println("类型:" + type); 56 System.out.println("内容:" + body); 57 System.out.println("======================"); 58 } 59 cursor.close(); 60 } 61 62 63 private void insertMsg() { 64 ContentResolver resolver = getContentResolver(); 65 Uri uri = Uri.parse("content://sms/"); 66 ContentValues conValues = new ContentValues(); 67 conValues.put("address", "123456789"); 68 conValues.put("type", 1); 69 conValues.put("date", System.currentTimeMillis()); 70 conValues.put("body", "no zuo no die why you try!"); 71 resolver.insert(uri, conValues); 72 Log.e("HeHe", "短信插入完毕~"); 73 } 74 }