android利用ContentResolver訪問者獲取手機短信信息
利用ContentResolver訪問者獲取手機短信信息,在此記錄一下,一遍以后查詢。
首先看一下結(jié)果,結(jié)果如下:
activity_message.xml類:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_message" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android_25.MessageActivity"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lv_message" > </ListView> </LinearLayout>
activity_xs.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_xs" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android_25.XsActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_name" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_telephone" /> </LinearLayout>
MessageActivity類:
public class MessageActivity extends AppCompatActivity { private ListView lv_message; private ContentResolver cr; private ArrayList<Map<String, Object>> datalistView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_message); //獲得短信的ID lv_message = (ListView) findViewById(R.id.lv_message); //得到訪問者ContentResolver cr = getContentResolver(); //定義一個接收短信的集合 datalistView = new ArrayList<>(); Uri uri = Uri.parse("content://sms/"); Cursor cursor = cr.query(uri, null, null, null, null); while (cursor.moveToNext()) { String body = cursor.getString(cursor.getColumnIndex("body")); int address = cursor.getInt(cursor.getColumnIndex("address")); //將號碼和短信內(nèi)容放入Map集合中 Map<String, Object> map = new HashMap<>(); map.put("images", address+""); map.put("titles", body); datalistView.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, datalistView, R.layout.activity_xs, new String[]{"images", "titles"}, new int[]{R.id.tv_name, R.id.tv_telephone}); lv_message.setAdapter(adapter); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實現(xiàn)帶指示器的自動輪播式ViewPager
這篇文章主要為大家詳細介紹了Android實現(xiàn)帶指示器的自動輪播式ViewPager的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android LayoutInflater.inflate源碼分析
這篇文章主要介紹了Android LayoutInflater.inflate源碼分析的相關(guān)資料,需要的朋友可以參考下2016-12-12詳解Android Libgdx中ScrollPane和Actor事件沖突問題的解決辦法
這篇文章主要介紹了詳解Android Libgdx中ScrollPane和Actor事件沖突問題的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09Android開發(fā)Kotlin語言協(xié)程中的并發(fā)問題和互斥鎖
Android開發(fā)Kotlin語言提供了多種機制來處理并發(fā)和同步,其中包括高層次和低層次的工具,對于常規(guī)的并發(fā)任務(wù),可以利用 Kotlin 協(xié)程提供的結(jié)構(gòu)化并發(fā)方式,而對于需要更低層次的鎖定機制,可以使用Mutex(互斥鎖)來實現(xiàn)對共享資源的線程安全訪問2024-06-06

android12?SD如何動態(tài)申請讀寫權(quán)限

Android開發(fā)實現(xiàn)的幾何圖形工具類GeometryUtil完整實例