Android权限ACCESS_FINE_LOCATION始终被拒绝

【Android权限ACCESS_FINE_LOCATION始终被拒绝】识字粗堪供赋役,不须辛苦慕公卿。这篇文章主要讲述Android权限ACCESS_FINE_LOCATION始终被拒绝相关的知识,希望能为你提供帮助。
我尝试检查按钮点击的权限,它总是给我-1
这是我的androidManifest文件:

< ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.natalia.assignment_2"> < uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> < uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> < application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> < activity android:name=".MainActivity"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < /application> < /manifest>

我也有compileSdkVersion 28,minSdkVersion 22和targetSdkVersion 28。
在我的MainActivity中,我尝试检查权限并始终拒绝,因为它返回-1
public class MainActivity extends AppCompatActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button buttonLocate = (Button) findViewById(R.id.buttonLocate); buttonLocate.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { askForPermission("android.permission.ACCESS_FINE_LOCATION"); }); }private void askForPermission(String permission) {if(MainActivity.this != null) { if (PermissionChecker.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {// IT ALWAYS COMES TO THIS IF AND SHOWS -1 Toast.makeText(this, "" + String.valueOf(PermissionChecker.checkSelfPermission(MainActivity.this, permission)), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show(); } } }

}
我尝试了很多个小时,但没有任何帮助......这里有什么问题?
答案
askForPermission( “android.permission.ACCESS_FINE_LOCATION”);
这是你传递给你的方法时所拥有的。尝试做:
askForPermission("Manifest.permission.ACCESS_FINE_LOCATION");

这可能有效。如果没有,我检查了一些其他文章和我看过的任何地方,它没有作为字符串传递。
尝试使用没有字符串:
(PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)

希望它有效!

    推荐阅读