我正在关注Pokemon Go
app的示例,以显示喜欢的窗口消息
您需要[位置,相机,...]访问才能使用此应用程序。请允许访问此应用程序[设置]。单击[取消]退出此应用程序
当我点击设置时,应用程序将转到此应用程序的设置窗口,我可以接受所有权限。
可以使用代码
访问“设置”窗口public void gotoSetting(){
final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + getApplicationContext().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);
}
我的permisions存储在String数组
中 String[] PERMISSIONS = {Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA}
我使用此代码检查是否已检查所有权限,否则返回false
public static boolean hasAllPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
但是,我遇到了一个问题,即我没有检查所有权限,但应用程序没有再次进入设置。另外,如果用户没有检查所有权限,我可以显示一条窗口消息来通知。最后,这是我的代码
private AlertDialog buildNotificationServiceAlertDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Pemissions Setting");
alertDialogBuilder.setMessage("You need to [Location, Camera,...] access to use this application. Please allow access in this application [Setting]. Click [Cancel] to exit from this application");
alertDialogBuilder.setPositiveButton("Setting",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
gotoSetting();
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// If you choose to not enable the notification listener
// the app. will not work as expected
}
});
return(alertDialogBuilder.create());
}
在onCreate中我有
private AlertDialog enableNotificationListenerAlertDialog;
if(!hasAllPermissions(this, PERMISSIONS)){
enableNotificationListenerAlertDialog = buildNotificationServiceAlertDialog();
enableNotificationListenerAlertDialog.show();
}
更新:已解决:。我错过了调用hasAllPermissions
函数中的onResume()
。最后,我的解决方案是
@Override
protected void onResume() {
super.onResume();
if(hasAllPermissions(this, PERMISSIONS)==false){
enableNotificationListenerAlertDialog = buildNotificationServiceAlertDialog();
enableNotificationListenerAlertDialog.show();
}
}
答案 0 :(得分:0)
我仔细阅读了你的问题,我注意到你的第一行代码中有一个错误。由于你的代码以“public voide gotoSetting()”开头,错误在于关键字拼写。通过纠正,它可以写成“ public void gotoSetting()“。
我希望这会对你有所帮助。