Android利用ContentProvider讀取短信內容
本文實例為大傢分享瞭Android利用ContentProvider讀取短信內容的具體代碼,供大傢參考,具體內容如下
首先,我們來看下運行效果
運行效果如下:
展示短信內容的效果如下:
佈局文件(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="獲取手機所有的短信內容" android:onClick="getContactsSms" /> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_sms_list" ></ListView> </LinearLayout>
一個簡單的讀取短信內容的例子(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); //通知適配器發生改變 simpleAdapter.notifyDataSetChanged(); } } } }
最後需要在清單文件上配置讀取短信的權限即可(AndroidManifest.xml)
<uses-permission android:name="android.permission.READ_SMS" />
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Android ListView 實例講解清晰易懂
- Android使用ContentProvider實現查看系統短信功能
- Android 控件GridView使用案例講解
- 使用ListView實現網上訂餐首頁
- Android實現一個簡單的單詞本