gpt4 book ai didi

java - 无法即时看到危险权限的对话框

转载 作者:行者123 更新时间:2023-12-02 02:37:54 27 4
gpt4 key购买 nike

我尝试在我的应用上测试我的 GPS 权限。

我已经构建了权限管理器并在 list 中添加了 GPS 权限,因为 GPS 是一个危险的权限。但是,当我运行我的应用程序时,我看不到此权限的任何对话框,因为应用程序向我表示我没有允许此权限,即使我从来没有机会在对话框上允许或阻止此权限。

非常感谢任何形式的帮助,这些是我的类(class):

list 权限

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

主要 Activity

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);
}
}
}

最佳答案

您应该从 Manifest.permission 类中获取一个字符串。但是您将 Manifest.permission.ACCESS_FINE_LOCATION 作为字符串传递。你不应该使用这个。

public static final String gpsFinePermission="Manifest.permission.ACCESS_FINE_LOCATION";

使用这个。

public static final String gpsFinePermission= Manifest.permission.ACCESS_FINE_LOCATION;

关于java - 无法即时看到危险权限的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45978309/

27 4 0
文章推荐: java - 从不同的类调用 TextToSpeech
文章推荐: java - 如何使用 hibernate 在不迭代的情况下将 List 插入数据库?