Android 运行 时间权限 - onRequestPermissionsResult 回调未在 activity 中触发
Android run time permissions - onRequestPermissionsResult callback is not triggering in an activity
我正在尝试使用 SmsManager 发送短信。下面是我的代码,遵循 android 文档。但问题是,在按下权限 window 上的 DENY/ALLOW 按钮后,它不会触发 onRequestPermissionsResult 回调。我什至在 post 之一中根据某人的建议添加了 super.onRequestPermissionsResult。我已经通过设置断点在调试模式下使用日志语句进行了测试,但是根本没有调用 onRequestPermissionsResult。因此,即使在授予权限后,第一条短信也不会发出,"SMS sent. 1" 也不会显示。我错过了什么?什么情况下不触发回调?
private void sendSMSMessage() {
phoneNo = "+919535000000";
message = "Test SMS message";
if (ContextCompat.checkSelfPermission(NewAppointment.this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted. Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(NewAppointment.this,
Manifest.permission.SEND_SMS)) {
Toast.makeText(NewAppointment.this,
"Send SMS permission is needed to send notifications.", Toast.LENGTH_LONG).show();
}
// Request the permission
ActivityCompat.requestPermissions(NewAppointment.this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
} else {
// Permission has already been granted
message = "Test SMS message 2";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent. 2",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.i(TAG,"In onRequestPermissionsResult callback ---> ");
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
message = "Test SMS message 1";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent. 1",
Toast.LENGTH_LONG).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
我的错误。 activity 甚至在触发 onRequestPermissionsResult 之前就被杀死了。对代码进行了更改,以便 activity 不会被杀死。
我正在尝试使用 SmsManager 发送短信。下面是我的代码,遵循 android 文档。但问题是,在按下权限 window 上的 DENY/ALLOW 按钮后,它不会触发 onRequestPermissionsResult 回调。我什至在 post 之一中根据某人的建议添加了 super.onRequestPermissionsResult。我已经通过设置断点在调试模式下使用日志语句进行了测试,但是根本没有调用 onRequestPermissionsResult。因此,即使在授予权限后,第一条短信也不会发出,"SMS sent. 1" 也不会显示。我错过了什么?什么情况下不触发回调?
private void sendSMSMessage() {
phoneNo = "+919535000000";
message = "Test SMS message";
if (ContextCompat.checkSelfPermission(NewAppointment.this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted. Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(NewAppointment.this,
Manifest.permission.SEND_SMS)) {
Toast.makeText(NewAppointment.this,
"Send SMS permission is needed to send notifications.", Toast.LENGTH_LONG).show();
}
// Request the permission
ActivityCompat.requestPermissions(NewAppointment.this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
} else {
// Permission has already been granted
message = "Test SMS message 2";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent. 2",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.i(TAG,"In onRequestPermissionsResult callback ---> ");
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
message = "Test SMS message 1";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent. 1",
Toast.LENGTH_LONG).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
我的错误。 activity 甚至在触发 onRequestPermissionsResult 之前就被杀死了。对代码进行了更改,以便 activity 不会被杀死。