How to realize the function of viewing system SMS with ContentProvider in Android
This article mainly introduces "how Android uses ContentProvider to view system SMS function". In daily operation, I believe many people have doubts about how Android uses ContentProvider to view system SMS function. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "how to view system SMS function with Android using ContentProvider". Next, please follow the editor to study!
Activity_main.xml
Sms.java
Public class Sms {private int id; private String address; private String body; public Sms (int id, String address, String body) {this.id = id; this.address = address; this.body = body;} public int getId () {return id;} public void setId (int id) {this.id = id;} public String getAddress () {return address } public void setAddress (String address) {this.address = address;} public String getBody () {return body;} public void setBody (String body) {this.body = body;}}
MainActivity.java
Public class MainActivity extends AppCompatActivity {private TextView tv_show; private Button bt_show; private List list=new ArrayList (); private String text= ""; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); tv_show=findViewById (R.id.tv_show); bt_show=findViewById (R.id.bt_show) } public void show (View view) {ActivityCompat.requestPermissions (MainActivity.this,new String [] {Manifest.permission.READ_SMS}, 1);} public void getSms () {Uri uri= Uri.parse ("content://sms/"); ContentResolver contentResolver=getContentResolver () Cursor cursor = contentResolver.query (uri, new String [] {"_ id", "address", "body"}, null, null, null); if (cursorsor.getCount () > 0) {if (listings empty null) {list.clear ();} text= "" While (cursor.moveToNext ()) {int id=cursor.getInt (0); String name=cursor.getString (1); String body=cursor.getString (2); Sms sms=new Sms (id,name,body); list.add (sms);} for