gpt4 book ai didi

android - 短信权限

转载 作者:行者123 更新时间:2023-11-29 16:42:24 25 4
gpt4 key购买 nike

我正在通过 android studio 构建一个 Android 应用程序。该应用程序的一个主要功能是发送短信,但由于权限错误,我似乎无法让它在不同的设备上正确运行。我试图通过在用户登录时提示权限来消除此问题,但这适用于 API 版本 13 和26 日失败,我用 build.versioncode.o. 尝试过,但他也失败了。执行此操作并在每次发送短信时检查权限的正确方法是什么?(请注意,短信是从应用程序中的许多不同功能发送的。

我在发送消息时也遇到了这个错误

java.lang.SecurityException: Neither user 10205 nor current process has android.permission.READ_PHONE_STATE.

请求登录权限

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
private void getPermissionToReadSMS() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(
android.Manifest.permission.READ_SMS)) {
Toast.makeText(this, "Please allow permission!", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{android.Manifest.permission.READ_SMS},
REQUEST_READ_SMS);
}
}

发送方法的方法 fragment

    SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(testContact.getNumber(), null, "SENT MESSAGE" + message, sentPending, deliveredPending);

应用 list

 <!-- grant permission to uses in build sms service -->
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission-group.SMS" />

最佳答案

要检查发送短信的权限,您需要添加 <uses-permission android:name="android.permission.SEND_SMS" />>=23 api 的权限和以下代码

 protected void sendSMS() {
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},
PERMISSION_REQUEST_SEND_SMS);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent successfully.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"SMS faild", Toast.LENGTH_LONG).show();
return;
}
}
}

}

关于android - 短信权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50061698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com