Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ContentProvider to read SMS content in Android

2025-01-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how Android uses ContentProvider to read text messages. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

First of all, let's take a look at the running effect.

The running effect is as follows:

The effect of displaying SMS message content is as follows:

Layout file (activity_sms.xml)

A simple example of reading text messages (SMSActivity)

Package com.example.administrator.myapplication;import android.content.ContentResolver;import android.database.Cursor;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.ListView;import android.widget.SimpleAdapter;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Map Public class SMSActivity extends AppCompatActivity {private ListView lv_sms_list; private List data; private SimpleAdapter simpleAdapter; private ContentResolver contentResolver; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_sms); / / get visitor contentResolver = getContentResolver (); lv_sms_list = (ListView) findViewById (R.id.lv_sms_list); data = new ArrayList () / / Adapter simpleAdapter = new SimpleAdapter (this,data,android.R.layout.simple_list_item_2,new String [] {"names", "note"}, new int [] {android.R.id.text1,android.R.id.text2}); lv_sms_list.setAdapter (simpleAdapter) } public void getContactsSms (View view) {/ / read all SMS messages Uri uri=Uri.parse ("content://sms/"); ContentResolver resolver = getContentResolver (); Cursor cursor = resolver.query (uri, new String [] {"_ id", "address", "body", "date", "type"}, null, null, null) If (cursorial cursor.getCount () > 0) {int _ id; String address; String body; String date; int type; while (cursor.moveToNext ()) {Mapmap=new HashMap (); _ id=cursor.getInt (0); address=cursor.getString (1) Body=cursor.getString (2); date=cursor.getString (3); type=cursor.getInt (4); map.put ("names", body); data.add (map); Log.i ("test", "_ id=" + _ id+ "address=" + address+ "body=" + body+ "date=" + date+ "type=" + type) / / notify the adapter that simpleAdapter.notifyDataSetChanged ();}

Finally, configure the permission to read SMS messages on the manifest file (AndroidManifest.xml)

After reading the above, do you have any further understanding of how Android uses ContentProvider to read text messages? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 259

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report