运行一个应用的时候在其他系统上跑的是正常的,但是放到一个Andriod6.0的机子上就直接闪退了,
看了下日志,发现这句提示
ATAL EXCEPTION: mainProcess: ..****, PID: 12847java.lang.SecurityException: Sending SMS message: uid 10092 does not have android.permission.SEND_SMS
根据提示,应该是说我没有SEND_SMS权限,于是去检查了AndroidManifest.xml,发现已经加过了
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
为什么会出现这个问题呢,原因在于Android6.0后针对没有root的机器有更加严格的权限控制请求。具体权限列表如下:
ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ADD_VOICEMAIL
BODY_SENSORS
CALL_PHONE
CAMERA
GET_ACCOUNTS
PROCESS_OUTGOING_CALLS
READ_CALENDAR
READ_CALL_LOG
READ_CELL_BROADCASTS
READ_CONTACTS
READ_EXTERNAL_STORAGE
READ_PHONE_STATE
READ_SMS
RECEIVE_MMS
RECEIVE_SMS
RECEIVE_WAP_PUSH
RECORD_AUDIO
SEND_SMS
USE_SIP
WRITE_CALENDAR
WRITE_CALL_LOG
WRITE_CONTACTS
WRITE_EXTERNAL_STORAGE
当然解决办法也有3种:
1)修改编译的版本为23.0以下 例如
targetSdkVersion 21
2)舍弃IMEI 的获取方式,更换为获取mac
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imeiCode =null;
try {
imeiCode=manager.getDeviceId();
}catch(Exception e){
imeiCode=getMacAddress(context);
}
- 使用
checkSelfPermission
和requestPermissions
检查和请求权限
private static String[] PERMISSION= {Manifest.permission.READ_PHONE_STATE};
private boolean islacksOfPermission(String permission){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
return ContextCompat.checkSelfPermission(mContext, permission) ==
PackageManager.PERMISSION_DENIED;
}
return false;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==0x12){
setDeviceId();
}else{
finish();
}
}
Override
protected void onResume() {
super.onResume();
if(islacksOfPermission(PERMISSION[0])){
ActivityCompat.requestPermissions(this,PERMISSION,0x12);
}else{
setDeviceId();
}
}
private void setDeviceId(){
device_Id=getDeviceId();
app.setDeviceID(device_Id);
}
private String device_Id="";
private String getDeviceId(){
return Utils.getDeviceImei(mContext);
}
如果为了提升体验,也可以提示对话框让用户进行选择。
网友评论