我阅读并尝试过的有关SMS权限的所有示例都有效通过弹出窗口向用户寻求授权(表示操作可能会收取成本等)。如前所述,用户可以远离电话,因此无需用户授权即可在所需时间发送短信。
有没有办法获得授权,避免在发送时重新询问?
清单中声明的权限:
<uses-permission android:name="android.permission.SEND_SMS" />
短信发送
SmsManager smsMgr = SmsManager.getDefault();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);
smsMgr.sendTextMessage(Number, null, "txt", sentPI, deliveredPI);
感谢所有
答案 0 :(得分:0)
你需要在运行时获得android 6+的许可。这样做
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int permissionCheck = ContextCompat.checkSelfPermission(RegisterActivity.this, Manifest.permission.SEND_SM);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(RegisterActivity.this, new String[]{Manifest.permission.SEND_SM}, 1);
} else {
sendSMS();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
sendSMS();
} else {
finish();
}
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}