简体   繁体   English

调用SmsManager.sendTextMessage()时,Android 8需要READ_PHONE_STATE

[英]Android 8 requires READ_PHONE_STATE when calling SmsManager.sendTextMessage()

My application can't send sms on new android 8 update. 我的应用程序无法在新的Android 8更新上发送短信。 I get error that I don't have READ_PHONE_STATE permission. 我收到我没有READ_PHONE_STATE权限的错误。

java.lang.SecurityException: Neither user 10179 nor current process has android.permission.READ_PHONE_STATE.
    at android.os.Parcel.readException(Parcel.java:1942)
    at android.os.Parcel.readException(Parcel.java:1888)
    at com.android.internal.telephony.ISms$Stub$Proxy.sendTextForSubscriber(ISms.java:789)
    at android.telephony.SmsManager.sendTextMessageInternal(SmsManager.java:329)
    at android.telephony.SmsManager.sendTextMessage(SmsManager.java:312)
    at com.cordova.plugins.sms.Sms.send(Sms.java:192)
    at com.cordova.plugins.sms.Sms.access$400(Sms.java:22)
    at com.cordova.plugins.sms.Sms$1.run(Sms.java:102)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
    at java.lang.Thread.run(Thread.java:764)

usually this kind of error is fine - I just realise I need a permission and ask for it. 通常,这种错误是可以的-我只是意识到我需要获得许可并提出要求。 But in this case I can't find any documentation that I should need READ_PHONE_STATE to use SmsManager. 但在这种情况下,我无法找到任何文件,我应该需要READ_PHONE_STATE使用SmsManager。 Neither here sendTextMessage nor in new Android 8 update notes . 这里没有sendTextMessage也没有在新的Android 8 更新说明中 The latter one mentions that to get hardware serial number I now need READ_PHONE_STATE permission. 后者提到要获取硬件序列号,我现在需要READ_PHONE_STATE权限。

Researching further I found that many other people have ran into this issue, but no one has any details or solutions beyond just asking for said permission. 进一步研究发现,很多其他人都遇到了这个问题,但是除了征得上述许​​可之外,没有人有任何其他细节或解决方案。

So today I recreated the problem with simple pure application that just asks for SEND_SMS permission and sends sms. 因此,今天我用简单的纯应用程序重新创建了问题,该应用程序仅要求SEND_SMS许可并发送短信。 And got the exact same problem. 并得到了完全相同的问题。 It works on everything below Android 8. But crashes with permission error on android 8. Here is source code if anyone wants to recreate it. 它适用于Android 8以下的所有版本,但在Android 8上因权限错误而崩溃。如果有人要重新创建,则此处为源代码。

 compileSdkVersion 26 buildToolsVersion "26.0.2" defaultConfig { applicationId "com.example.usr.smstest" minSdkVersion 21 targetSdkVersion 26 } 
package com.example.usr.smstest;

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;

public class MainActivity extends AppCompatActivity {

    private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 1;

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    sendSms();
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.SEND_SMS)
                != PackageManager.PERMISSION_GRANTED) {

                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.SEND_SMS},
                        MY_PERMISSIONS_REQUEST_SEND_SMS);
        }else{
            sendSms();
        }
    }

    private void sendSms(){
        SmsManager manager = SmsManager.getDefault();
        manager.sendTextMessage("22222222", null, "msg", null, null);
    }

}

Submitted it on google issue tracker Google问题追踪器上提交了它

This is a bug in android O that so much annoying. 这是一个令人讨厌的android O中的错误。 if you check SmsManager.java you can see getSubscriptionId method that in body need READ_PHONE_STATE_PERMISSION and if you don't give READ_PHONE_STATE throw SecurityException 如果您检查SmsManager.java ,则可以看到getSubscriptionId方法在主体中需要READ_PHONE_STATE_PERMISSION并且如果您不给READ_PHONE_STATE抛出SecurityException

So all you can do is generate READ_PHONE_STATE and explain it to play store if you've been warned or wait to fix by google developers 因此,您所要做的就是生成READ_PHONE_STATE并解释它是否可以在商店中发出警告(如果您已被警告或等待Google开发人员修复)

You need to check the permissions in android nougat devices.

 if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
         != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
               Manifest.permission.SEND_SMS)) {
            } else {
               ActivityCompat.requestPermissions(this,
                  new String[]{Manifest.permission.SEND_SMS},
                  MY_PERMISSIONS_REQUEST_SEND_SMS);
            }
      }

Link - https://www.tutorialspoint.com/android/android_sending_sms.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM