6

android运行时授权读取短信后仍收不到短信

 1 year ago
source link: https://www.daozhao.com/10853.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

android运行时授权读取短信后仍收不到短信

如果您发现本文排版有问题,可以先点击下面的链接切换至老版进行查看!!!

android运行时授权读取短信后仍收不到短信

最近想给自己做一个短息转发的功能,这就需要读取短信的权限了,这里就用到了

代码里面这样写到 AndroidManifest.xml文件

<receiver android:name=".service.SmsReceiver"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
</receiver>

SmsReceiver文件

class SmsReceiver : BroadcastReceiver() {

    init {
        Log.v("dimos", "SmsReceiver create")
    }

    override fun onReceive(context: Context, intent: Intent) {
        val body: String = SmsHelper.getSmsBody(intent)
        val address: String = SmsHelper.getSmsAddress(intent)
        Log.v("sms received", "$address,$body")
        showSms(context, "短信源自$address", body)
    }
}

默认情况下,app是没有获取读取短信权限的

file

在模拟器发送测试短信

file

app是收不到短信消息的,除非手动将app赋予读取短信的权限。

file

我们总不能让用户自己在手机设置里面来给自己的app授权吧,android允许我们在app运行时弹出读取短信授权弹窗,提示用户主动授权。

private fun onReadSms() {
        when {
            ActivityCompat.checkSelfPermission(
                mContext!!,
                Manifest.permission.READ_SMS
            ) == PackageManager.PERMISSION_GRANTED -> {
                Log.i(CONST.PERMISSIONS_TAG, "READ_SMS_NO_PERMISSION")
                readSms()
            }
            shouldShowRequestPermissionRationale(Manifest.permission.READ_SMS) -> {
                Log.i(CONST.PERMISSIONS_TAG, "showUI")
                activityResultLauncher.launch(Manifest.permission.READ_SMS)
            }
            else -> {
 activityResultLauncher.launch(Manifest.permission.READ_SMS)
            }
        }
}
file

用户授权后,系统显示app有读取短信的权限了。

file

然后再次用模拟器发送测试短信,但是app确收不到消息了。。。

file

难道说,即使设置里面显示的是允许读取短信,但是在设置页手动授权和app运行授权的还是有区别的,系统还是会记录这个授权途径的吗。

更新时间:2023-03-08 07:28
上一篇:下一篇:

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK