0
我試圖在我的應用上測試我的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);
}
}
}