我已经检查了已经发布的解决方案,以解决建议我放入权限检查的错误。
使用targetSdkVersion 26-和Android Studio 3.2
我在YouTube应用程序中设置了一条短信(来自youtube教程)。我在两个单独的设备上有两个应用程序。我希望App A的用户能够与App B的用户进行交流。我在本教程的两个App中都输入了相同的代码
Sending and Receiving Messages or SMS - Android Studio Tutorial
首先,当我运行应用程序时,出现错误:
Sending SMS Message: uid 10085 does not have android.permission.SEND_SMS
因此,我在按钮onclick()中添加了权限检查
现在,当我运行应用程序时,尝试发送时没有任何反应。
清单
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" android:protectionLevel="signature" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activities.RiderActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MessageReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
SMS_Activity
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// display the message in the textView
TextView inTxt = (TextView) findViewById(R.id.textMsg);
inTxt.setText(intent.getExtras().getString("message"));
}
};
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String myMsg = tvMessage.getText().toString();
String theNumber = tvNumber.getText().toString();
if (ContextCompat.checkSelfPermission(SMS_Activity.this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
} else {
sendMsg(theNumber, myMsg);
}
}
});
protected void sendMsg(String theNumber, String myMsg) {
String SENT = "Message Sent";
String DELIVERED = "Message Delivered";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new
Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(theNumber, null, myMsg, sentPI, null);
}
添加了onRequestPermissionsResult:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (permissions[0].equalsIgnoreCase
(Manifest.permission.SEND_SMS)
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
// Permission was granted.
} else {
// Permission denied. Stop the app.
Log.d(TAG, getString(R.string.failure_permission));
Toast.makeText(this,
getString(R.string.failure_permission),
Toast.LENGTH_SHORT).show();
}
}
}
}
更新了btnSend.setOnClickListener:
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String myMsg = tvMessage.getText().toString();
String theNumber = tvNumber.getText().toString();
if (ContextCompat.checkSelfPermission(SMS_Activity.this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
ActivityCompat.requestPermissions(SMS_Activity.this,
new String[]{Manifest.permission.SEND_SMS}, 1);
sendMsg(theNumber, myMsg);
}
}
});
按照建议,我遵循了本教程:
Android Apps - Phone Calls and SMS
给我这个错误:
java.lang.IllegalStateException: Could not execute method for android:onClick
Caused by: java.lang.SecurityException: Neither user 10085 nor current process has android.permission.READ_PHONE_STATE.
然后我进行了以下更改:
AndroidManifest-添加:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
SMSActivity-添加:
private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 2;
... checkforPermissions()//在onCreate且在smsManager之前运行
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS) !=
PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, getString(R.string.permission_not_granted));
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE) !=
PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, getString(R.string.permission_not_granted));
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_PHONE_STATE},
MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}
} else {
// Permission already granted. Enable the SMS button.
enableSmsButton();
}
... onRequestPermissionsResult()
// Check if permission is granted or not for the request.
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (permissions[0].equalsIgnoreCase
(Manifest.permission.SEND_SMS)
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
// Permission was granted. Enable sms button.
enableSmsButton();
} else {
// Permission denied.
Log.d(TAG, getString(R.string.failure_permission));
Toast.makeText(this,
getString(R.string.failure_permission),
Toast.LENGTH_LONG).show();
// Disable the sms button.
disableSmsButton();
}
}
break;
case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
if (permissions[0].equalsIgnoreCase
(Manifest.permission.READ_PHONE_STATE)
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
// Permission was granted. Enable sms button.
enableSmsButton();
} else {
// Permission denied.
Log.d(TAG, getString(R.string.failure_permission));
Toast.makeText(this,
getString(R.string.failure_permission),
Toast.LENGTH_LONG).show();
// Disable the sms button.
disableSmsButton();
}
}
break;
default:
break;
}
现在,当我运行应用程序时,出现与上述相同的错误。
已解决的问题
SMSACTIVITY
private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
private boolean checkAndRequestPermissions() {
int permissionSendSms = ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS);
int permissionReceiveSms = ContextCompat.checkSelfPermission(this,
Manifest.permission.RECEIVE_SMS);
int permissionReadPhoneState = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE);
List<String> listPermissionsNeeded = new ArrayList<>();
if (permissionSendSms != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
}
if (permissionReceiveSms != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.RECEIVE_SMS);
}
if (permissionReadPhoneState != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new
String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
// Check if permission is granted or not for the request.
if (grantResults.length == 0 || grantResults == null) {
/*If result is null*/
disableSmsButton();
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
/*If We accept permission*/
if (getIntent() != null) {
phone_no = getIntent().getStringExtra("phone");
Log.e(TAG, "phoneNo = " + phone_no);
}
enableSmsButton();
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
/*If We Decline permission*/
disableSmsButton();
}
}
}
}
现在一切都很好!
答案 0 :(得分:2)
目前,您的应用尚未授予短信许可,因此它正在运行// Permission is not granted
,这是一段相当平稳的代码行。您可以将ActivityCompat API与
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.SEND_SMS},
1
);
您可以使用onRequestPermissionsResult来捕获此事件,然后如果他们给您访问权限,则继续正常进行;如果他们拒绝访问,则显示Toast或其他内容。