我最近更新了我的应用程序,以支持android 6棉花糖。我遵循了关于https://developer.android.com/training/permissions/requesting.html的指令
并为requestPermissions添加了Manifest.permission.RECEIVE_SMS。当im运行以下代码时:
Log.i(TAG, "sending SMS...");
Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVED");
intent.putExtra("pdus", data);
getContext().sendOrderedBroadcast(intent, null);我得到了
java.lang.SecurityException:权限拒绝:不允许从pid=1999,uid=10056发送广播android.provider.Telephony.SMS_RECEIVED
即使我授予SMS_RECEIVED许可,我也不能在设备上发送短信广播。
知道我为什么会在android 6上得到这个安全异常吗?
我的目标是在我的设备链接[can I send "SMS received intent"?]中生成一条假短信。我在谷歌上没有发现任何不被允许的地方。
发布于 2016-11-12 02:15:38
您需要将权限添加到清单xml中:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>和,您需要在运行时请求权限。直到android 6,安装时才自动授予权限。在android 6和更高版本中,您可以安装应用程序而不授予权限。您可以在活动类中使用此函数:
private void requestSmsPermission() {
String permission = Manifest.permission.RECEIVE_SMS;
int grant = ContextCompat.checkSelfPermission(this, permission);
if ( grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
}这-你的活动。
发布于 2015-10-26 16:17:27
Android6运行时权限android.provider.Telephony.SMS_RECEIVED允许您在系统短消息提供程序发送消息时接收接收该消息。
然而,你却试图自己传播这一信息。我不确定这是允许的,正如你已经发现的,并不是由相同的权限控制的。(事实上,我假设它已经被锁定在Marshmallow上,这样只有系统才能将收到的SMS消息通知应用程序)。
发布于 2016-05-25 05:05:15
您需要api级23+的权限,google重新修改了权限系统,以便应用程序用户可以在安装应用程序后授予和撤销权限。
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
if(Build.VERSION.SDK_INT < 23){
//your code here
}else {
requestContactPermission();
}
private void requestContactPermission() {
int hasContactPermission =ActivityCompat.checkSelfPermission(context,Manifest.permission.RECEIVE_SMS);
if(hasContactPermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(Context, new String[] {Manifest.permission.RECEIVE_SMS}, PERMISSION_REQUEST_CODE);
}else {
//Toast.makeText(AddContactsActivity.this, "Contact Permission is already granted", Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
// Check if the only required permission has been granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.i("Permission", "Contact permission has now been granted. Showing result.");
Toast.makeText(this,"Contact Permission is Granted",Toast.LENGTH_SHORT).show();
} else {
Log.i("Permission", "Contact permission was NOT granted.");
}
break;
}
}https://stackoverflow.com/questions/33347809
复制相似问题