给出在android中通过 ContentResolver 读取手机的所有短信内容的包括布局等在内详细完整代码
时间: 2024-12-11 16:35:41 浏览: 5
![PDF](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PDF.png)
android利用ContentResolver访问者获取手机短信信息
在Android中,你可以使用`ContentResolver`来访问联系人、短信等系统数据。以下是通过`ContentResolver`读取手机所有短信内容并展示在一个列表的详细示例代码。注意,此代码仅用于教学目的,实际应用中需要处理隐私权限和异常情况。
首先,确保你的应用已经有了读取短信的权限:
```java
// 在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.READ_SMS"/>
```
然后,在你的Activity或Fragment中获取`ContentResolver`实例:
```java
private static final String SMS_URI = "content://sms/inbox";
public class SmsListActivity extends AppCompatActivity {
private ListView smsListView;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_list);
smsListView = findViewById(R.id.sms_list_view);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<>());
getSms();
}
private void getSms() {
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(SMS_URI, null, null, null, "date DESC");
if (cursor != null) {
try {
int indexBody = cursor.getColumnIndex(PhoneContract.SmsColumns.BODY);
while (cursor.moveToNext()) {
String body = cursor.getString(indexBody);
adapter.add(body);
}
smsListView.setAdapter(adapter);
} finally {
cursor.close();
}
} else {
Log.d("SMS", "Failed to query SMS");
}
}
}
```
这里我们使用了`PhoneContract.SmsColumns`中的`BODY`字段来获取短信内容。布局方面,假设你有一个简单的`activity_sms_list.xml`,包含一个`ListView`:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SmsListActivity">
<ListView
android:id="@+id/sms_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
```
当用户滚动列表时,会显示从数据库获取的短信内容。记得在合适的地方处理权限请求,以及异常处理以确保用户体验。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)