The android studio content provider (the cat who checks the text message) clicks to view the text message, and it flashes back, and the content of the text message cannot be displayed.

The wrong question:

Permission Denial: readingcom.android.providers.telephony.SmsProvider uri content://sms/ 

Found the problem:

The teacher recently talked about the content provider, let us make a case, check the cat of the text message, but I finished writing the code, edited the text message in android studio, and there is no problem in checking the code, but the program crashed, and I found the above in Logcat Program error. I searched on the Internet and found out that it is a permission problem, and sdk23 has made changes to the application permissions.

Solution:

The blogger provided two ending methods:

1. Use sdk22 to develop, and the android virtual machine or mobile phone system running this program is below 6.0. (I don't recommend it)

2. Perform permission verification, add the code in MainActivity as shown below:

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;
    }
 }

Using the above method, after the emulator that comes with android studio I use is running, click to view the text message to view the text message I sent.

Attach the reference URL: http://t.csdn.cn/SC3kH

The following is the program source code:

1. Create a layout.XML.file

I use the RelativeLayout interface for the page, and the name is 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="The read system message is as follows:"
    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="view text messages"
    android:textSize="30sp"/>
</RelativeLayout>

In the above code, a Button is set, and its click event "readSMS" is defined. After clicking the button, the found SMS information will be displayed in the TextView.

2. Write an entity class:

Create an entity class SmsInfo, which is used to encapsulate the attributes of the SMS and store the information of a single SMS.

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. Write interface interaction code in MainActivity to realize the function of viewing system text messages.

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 +="cellphone number:"+smsInfos.get(i).getAddress()+"\n";
            text +="SMS content:"+smsInfos.get(i).getBody()+"\n\n";
            tvSms.setText(text);
        }
    }
}

4. Add permissions

In this case, the operation of reading text messages is performed, so the permission to read text messages needs to be added to the AndroidManifest.xml file. The code is as follows:

<uses-permission android:name="android.permission.READ_SMS"/>


Note that outside <application></application>

5. Send SMS

We need to make sure that the text message has been sent. The following is to check the mobile phone number of the emulator that comes with the android studio you installed.

send messages

 

6. Run the program

Run MainActivity, click the "View SMS" button, and you can see the SMS we sent!

 

Finally, if you want to implement Chinese input in the Android Studio virtual machine, you may wish to take a look at this article:

http://t.csdn.cn/bhmox                

The rivers and lakes are far away, let us meet at the top!

Tags: Android IDE Android Studio

Posted by aquilina on Fri, 09 Dec 2022 16:17:12 +0530