我尝试在我的应用上测试我的GPS权限。
我已经建立了权限管理器并在清单中添加了GPS权限,因为GPS是一种危险的权限。 但是,当我运行我的应用程序时,我看不到此权限的任何对话框, 因为该应用程序向我敬酒,即使我从未有机会在对话框中允许或阻止此权限,但我还未获得此许可。
感谢任何帮助,这些都是我的课程:
明确许可
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
MainActiviey
public class MainActivity extends AppCompatActivity {
public static final int GPS=1;
public static final String gpsFinePermission="Manifest.permission.ACCESS_FINE_LOCATION";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PermissionManager.check(MainActivity.this, gpsFinePermission, GPS);
System.out.println("1");
}
@Override
protected void onStart() {
//startService(new Intent(this,CurrentLocation.class));
super.onStart();
System.out.println("2");
}
@Override//when user allowed OR denied a permission
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
System.out.println("3");
if(grantResults[0] == PackageManager.PERMISSION_GRANTED && requestCode == GPS){//Allowed
Toast.makeText(MainActivity.this, "GPS permission granted", Toast.LENGTH_LONG).show();
System.out.println("4");
}else
Toast.makeText(MainActivity.this, "GPS permission is IMPORTANT for this app", Toast.LENGTH_LONG).show();
}
}
权限管理员
public class PermissionManager {
//A method that can be called from any Activity, to check for specific permission
public static void check(Activity activity, String permission, int requestCode){
System.out.println("5");
//If requested permission isn't Granted yet
if (ActivityCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
System.out.println("6");
//Request permission from user
ActivityCompat.requestPermissions(activity,new String[]{permission},requestCode);
}
}
}
答案 0 :(得分:2)
你应该从Manifest.permission类中获取一个字符串。但是你将Manifest.permission.ACCESS_FINE_LOCATION
作为String传递。
你不应该使用它。
public static final String gpsFinePermission="Manifest.permission.ACCESS_FINE_LOCATION";
使用此功能。
public static final String gpsFinePermission= Manifest.permission.ACCESS_FINE_LOCATION;