报错的问题:
Permission Denial: readingcom.android.providers.telephony.SmsProvider uri content://sms/
找到问题:
老师最近讲到内容提供者,让我们做个案例,查看短信的猫,但我写完代码,在android studio里编辑了短信,而且检查代码没有问题,但出现程序闪退,我在Logcat发现上述程序错误。我在网上搜索得出是权限问题,是sdk23对应用程序权限做出了改动。
解决方法:
那个博主提供了两种结局方法:
1.使用sdk22开发,并且运行此程序的android虚拟机或者是手机系统6.0以下。(我不建议采用)
2.进行权限校验,在MainActivity里添加代码如下图:
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int hasReadSmsPermission = checkSelfPermission(Manifest.permission.READ_SMS);
if (hasReadSmsPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_SMS}, REQUEST_CODE_ASK_PERMISSIONS);
return;
}
}
用上面方法,我用的android studio自带的模拟器运行后,点击查看短信就可以查看我发出的短信了。
附上参考网址:http://t.csdn.cn/SC3kH
以下是程序源代码:
1.创建一个layout.XML.file
页面我采用RelativeLayout界面,名称为duanxin.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bg9"/>
<TextView
android:id="@+id/tv_des"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="20dp"
android:text="读取到的系统短信如下:"
android:textSize="30sp"
android:visibility="invisible"
/>
<TextView
android:id="@+id/tv_sms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_des"
android:lines="20"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="80dp"
android:layout_marginLeft="26dp"
android:background="#D9D1FA"
android:onClick="readSMS"
android:padding="5dp"
android:text="查看短信"
android:textSize="30sp"/>
</RelativeLayout>
在上述代码中,设置了一个Button,定义了他的单击事件“readSMS",单击按钮之后将查到的短信信息显示到TextView中。
2.编写一个实体类:
创建一个实体类SmsInfo,用于封装短信的属性,存储单条短信的信息。
public class SmsInfo {
private int _id;
private String address;
private int type;
private String body;
private long date;
public SmsInfo(int _id, String address, int type, String body, long date) {
this._id = _id;
this.address = address;
this.type = type;
this.body = body;
this.date = date;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
}
3.在MainActivity中编写界面交互代码,以实现查看系统短信的功能。
package com.example.filetext02;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity13 extends AppCompatActivity {
private TextView tvSms;
private TextView tvDes;
private String text = "";
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.duanxin);
tvSms=(TextView)findViewById(R.id.tv_sms);
tvDes=(TextView)findViewById(R.id.tv_des) ;
}
public void readSMS(View view){
Uri uri = Uri.parse("content://sms/");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
int hasReadSmsPermission = checkSelfPermission(Manifest.permission.READ_SMS);
if (hasReadSmsPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_SMS}, REQUEST_CODE_ASK_PERMISSIONS);
return;
}
}
ContentResolver resolver=getContentResolver();
Cursor cursor = resolver.query(uri,new String[]{"_id","address","type","body","date"},null,null,null);
List<SmsInfo>smsInfos = new ArrayList<SmsInfo>();
if(cursor !=null && cursor.getCount()>0){
tvDes.setVisibility(View.VISIBLE);
while (cursor.moveToNext()){
int _id=cursor.getInt(0);
String address = cursor.getString(1);
int type = cursor.getInt(2);
String body = cursor.getString(3);
long date = cursor.getLong(4);
SmsInfo smsInfo = new SmsInfo(_id,address,type,body,date);
smsInfos.add(smsInfo);
}
cursor.close();
}
for(int i =0;i<smsInfos.size();i++){
text +="手机号码:"+smsInfos.get(i).getAddress()+"\n";
text +="短信内容:"+smsInfos.get(i).getBody()+"\n\n";
tvSms.setText(text);
}
}
}
4.添加权限
该案例进行了读取短信的操作,因此需要在AndroidManifest.xml文件中加上读取短信的的权限,代码如下:
<uses-permission android:name="android.permission.READ_SMS"/>
注意在<application></application>外
扫描二维码关注公众号,回复:
14985726 查看本文章
5.发送短信
我们要确保已经发送了短信,下面是查看你安装的android studio自带的模拟器手机号码。
发送短信
6.运行程序
运行MainActivity,点击"查看短信"按钮,就可以看到我们发送的短信了!
最后如果你想在Android Studio 虚拟机(实现输入中文) ,不妨看一看这篇文章: