我已经应用了多个代码,但是它告诉我checkSelfPermission()和shouldShowRequestPermissionRationale方法没有退出,请帮助我..
我使用brodcast接收器类阅读短信它可以正常使用软糖到lolipop但不适合棉花糖和低于版本的冰淇淋像icecream sandwitch,我怎样才能使应用程序兼容所有Android版本,
请一步一步指导我,因为我是新手,请给我举例。
听到我的读取短信权限的代码..
public void getpermission(){
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(MainActivity,
Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_SMS},
REQUEST_CODE_ASK_SINGLE_PERMISSION);
// REQUEST_CODE_ASK_SINGLE_PERMISSION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
答案 0 :(得分:6)
你可以通过在你的Activity中扩展这个给定的类来实现它,
public class BasePermissionAppCompatActivity extends AppCompatActivity {
private final static String APP_NAME = "APP_NAME";
private final static int REQUEST_READ_SMS_PERMISSION = 3004;
public final static String READ_SMS_PERMISSION_NOT_GRANTED = "Please allow " + APP_NAME + " to access your SMS from setting";
RequestPermissionAction onPermissionCallBack;
private boolean checkReadSMSPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
} else {
return true;
}
}
public void getReadSMSPermission(RequestPermissionAction onPermissionCallBack) {
this.onPermissionCallBack = onPermissionCallBack;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!checkReadSMSPermission()) {
requestPermissions(new String[]{Manifest.permission.READ_SMS}, REQUEST_READ_SMS_PERMISSION);
return;
}
}
if (onPermissionCallBack != null)
onPermissionCallBack.permissionGranted();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (REQUEST_READ_SMS_PERMISSION == requestCode) {
// TODO Request Granted for READ_SMS.
System.out.println("REQUEST_READ_SMS_PERMISSION Permission Granted");
}
if (onPermissionCallBack != null)
onPermissionCallBack.permissionGranted();
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
if (REQUEST_READ_SMS_PERMISSION == requestCode) {
// TODO REQUEST_READ_SMS_PERMISSION Permission is not Granted.
// TODO Request Not Granted.
// This code is for get permission from setting.
//final Intent i = new Intent();
//i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//i.addCategory(Intent.CATEGORY_DEFAULT);
//i.setData(Uri.parse("package:" + getPackageName()));
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
//i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//startActivity(i);
}
if (onPermissionCallBack != null)
onPermissionCallBack.permissionDenied();
}
}
public interface RequestPermissionAction {
void permissionDenied();
void permissionGranted();
}
}
像这样
public class ActivityMain extends BasePermissionAppCompatActivity
现在在您的ActivityMain中,您可以通过此代码块获得权限
getReadSMSPermission(new RequestPermissionAction() {
@Override
public void permissionDenied() {
// Call Back, when permission is Denied
// TODO, task after permission is not greante
}
@Override
public void permissionGranted() {
// Call Back, when permission is Granted
// TODO, task after permission is greante
}
});
还有一件事,你需要在清单中添加权限
<uses-permission android:name="android.permission.READ_SMS"/>
答案 1 :(得分:1)
你应该阅读文件https://developer.android.com/training/permissions/requesting.html 所以,按照文件
private static finat int MY_PERMISSIONS_REQUEST_READ_SMS = 101;
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_SMS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_SMS},
MY_PERMISSIONS_REQUEST_READ_SMS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
,您必须处理权限请求响应。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_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.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
答案 2 :(得分:0)
使用以下方法:
public boolean checkPermissionSMS(final Context context, final String[] PERMISSIONS, final int Requestcode) {
int currentApiversion = Build.VERSION.SDK_INT;
if (currentApiversion >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_SMS)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("Read SMS permission is necessary");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//in fragment
requestPermissions(PERMISSIONS,Requestcode);
//in activity
ActivityCompact.requestPermissions(YourActivity.this,PERMISSIONS,Requestcode);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
//in fragment
requestPermissions(PERMISSIONS,Requestcode);
//in activity
ActivityCompact.requestPermissions(YourActivty.this,PERMISSIONS,Requestcode);
}
return false;
} else
return true;
} else
return true;
}
and then in onrequestPermissionresult:
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==100)
{
if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
{
//do your functionality
}
}
}
并调用这样的方法:
checkPermissionSMS(context,new String[]{Manifest.permission.READ_SMS},100);
当你请求这样的许可时:
requestPermissions(PERMISSIONS,Requestcode);
然后在调用片段
中搜索onRequestPermissionResult方法当你请求这样的许可时:
ActivityCompact.requestPermissions(YourActivity.this,PERMISSIONS,Requestcode);
然后在调用活动中搜索onRequestPermissionREsult方法。