我正在通过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);
}
}
发送方法的方法片段
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(testContact.getNumber(), null, "SENT MESSAGE" + message, sentPending, deliveredPending);
App Manifest
<!-- 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" />
答案 0 :(得分:2)
要使checkPermission发送短信,您需要添加
<uses-permission android:name="android.permission.SEND_SMS" />
权限和以下代码&gt; = 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;
}
}
}
}
答案 1 :(得分:1)
//You should declare this in your project
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_SMS,
Manifest.permission.RECEIVE_SMS,
Manifest.permission.SEND_SMS,
};
//并在您想要的地方调用verifymethod verifyStoragePermissions(Activity_home.this);
//and the verifyStoragePermissions methode is
public static void verifyStoragePermissions(Activity activity) {
int readPermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE);
int smspermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_SMS);
int smspermission1 = ActivityCompat.checkSelfPermission(activity, Manifest.permission.RECEIVE_SMS);
int smssendpermission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.SEND_SMS);
if (readPermission != PackageManager.PERMISSION_GRANTED || readPermission != PackageManager.PERMISSION_GRANTED || smspermission != PackageManager.PERMISSION_GRANTED || smspermission1 != PackageManager.PERMISSION_GRANTED || smssendpermission != PackageManager.PERMISSION_GRANTED) {userActivityCompat.requestPermissions(activity,PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE );
}
}