调用SmsManager.sendTextMessage()时,Android 8需要READ_PHONE_STATE [英] Android 8 requires READ_PHONE_STATE when calling SmsManager.sendTextMessage()

查看:154
本文介绍了调用SmsManager.sendTextMessage()时,Android 8需要READ_PHONE_STATE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序无法在新的android 8更新上发送短信. 我收到我没有 READ_PHONE_STATE 权限的错误消息.

My application can't send sms on new android 8 update. I get error that I don't have READ_PHONE_STATE permission.

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)

通常这种错误是可以的-我只是意识到我需要一个许可并提出要求.但是在这种情况下,我找不到需要 READ_PHONE_STATE 的任何文档才能使用SmsManager.这里都不是

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. Neither here sendTextMessage nor in new Android 8 update notes. The latter one mentions that to get hardware serial number I now need READ_PHONE_STATE permission.

进一步研究后,我发现很多其他人都遇到了这个问题,但是除了征得上述许​​可外,没有人有任何其他细节或解决方案.

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.

所以今天我用简单的纯应用程序重新创建了问题,该应用程序只要求获得 SEND_SMS 许可并发送短信.并得到了完全相同的问题.它可以在Android 8以下的所有版本上运行.但是在Android 8上会因权限错误而崩溃.如果有人想要重新创建,则为源代码.

So today I recreated the problem with simple pure application that just asks for SEND_SMS permission and sends 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.

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

}

通过 Google问题跟踪器提交

推荐答案

这是android O中的一个令人讨厌的错误.如果您检查 SmsManager .java 您可以看到体内需要 READ_PHONE_STATE_PERMISSION getSubscriptionId方法 如果您不给READ_PHONE_STATESecurityException

This is a bug in android O that so much annoying. 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

因此,您所能做的就是生成READ_PHONE_STATE,并在收到警告或等待Google开发人员修复后解释它在商店中播放

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

这篇关于调用SmsManager.sendTextMessage()时,Android 8需要READ_PHONE_STATE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆