Android利用ContentProvider讀取短信內(nèi)容
本文實(shí)例為大家分享了Android利用ContentProvider讀取短信內(nèi)容的具體代碼,供大家參考,具體內(nèi)容如下
首先,我們來看下運(yùn)行效果
運(yùn)行效果如下:
展示短信內(nèi)容的效果如下:
布局文件(activity_sms.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_sms" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.administrator.myapplication.SMSActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獲取手機(jī)所有的短信內(nèi)容" android:onClick="getContactsSms" /> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_sms_list" ></ListView> </LinearLayout>
一個(gè)簡(jiǎn)單的讀取短信內(nèi)容的例子(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<Map<String,Object>> data; private SimpleAdapter simpleAdapter; private ContentResolver contentResolver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sms); //獲取訪問者 contentResolver = getContentResolver(); lv_sms_list = (ListView) findViewById(R.id.lv_sms_list); data = new ArrayList<Map<String,Object>>(); //適配器 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) { //讀取所有短信 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(cursor!=null&&cursor.getCount()>0){ int _id; String address; String body; String date; int type; while (cursor.moveToNext()){ Map<String,Object>map=new HashMap<String,Object>(); _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); //通知適配器發(fā)生改變 simpleAdapter.notifyDataSetChanged(); } } } }
最后需要在清單文件上配置讀取短信的權(quán)限即可(AndroidManifest.xml)
<uses-permission android:name="android.permission.READ_SMS" />
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android 震動(dòng)和提示音的實(shí)現(xiàn)代碼
這篇文章主要介紹了android 震動(dòng)和提示音的實(shí)現(xiàn)代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12Android實(shí)現(xiàn)果凍滑動(dòng)效果的控件
這篇文章給大家主要介紹了利用Android如何實(shí)現(xiàn)果凍效果滑動(dòng)效果的控件,實(shí)現(xiàn)的效果類似于iOS有阻尼效果的滑動(dòng)控件,一般我們比較親切地稱之為果凍控件,常見的如微信里[我]的那個(gè)面板模塊,即使沒有再多的選項(xiàng),也不會(huì)很生硬的不允許用戶滑動(dòng)。下面來一起看看吧。2016-11-11在Android項(xiàng)目中使用AspectJ的方法
這篇文章主要介紹了在Android項(xiàng)目中使用AspectJ的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04Android編程實(shí)現(xiàn)左右滑動(dòng)切換背景的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)左右滑動(dòng)切換背景的方法,涉及Android圖形資源動(dòng)態(tài)調(diào)用與動(dòng)作監(jiān)聽相關(guān)技巧,需要的朋友可以參考下2016-01-01Android開發(fā)自學(xué)筆記(三):APP布局上
這篇文章主要介紹了Android開發(fā)自學(xué)筆記(三):APP布局上,本文講解了添加ViewGroup、添加ViewGroup、定義string內(nèi)容、添加Button、運(yùn)行程序查看效果等內(nèi)容,需要的朋友可以參考下2015-04-04Android GridView擴(kuò)展仿微信微博發(fā)圖動(dòng)態(tài)添加刪除圖片功能
這篇文章主要為大家詳細(xì)介紹了Android GridView擴(kuò)展仿微信微博發(fā)圖動(dòng)態(tài)添加刪除圖片功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android實(shí)現(xiàn)自定義View控件的流程詳解
這篇文章主要為大家詳細(xì)介紹了Android中實(shí)現(xiàn)自定義View控件的流程,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-06-06Android實(shí)現(xiàn)類似IOS右滑返回的效果(原因分析及解決辦法)
這篇文章主要介紹了Android實(shí)現(xiàn)類似IOS右滑返回的效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-03-03Android自定義實(shí)現(xiàn)頂部粘性下拉刷新效果
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)頂部粘性下拉刷新效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10