I try to check the permission on button click and it always gives me -1
Here is my AndroidManifest
file:
<?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>
Also i have compileSdkVersion
28, minSdkVersion
22 and targetSdkVersion
28.
In my MainActivity
, I try to check the permissions and it always denied, because it returns -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();
}
}
}
}
I tried many hours, but nothing helps...what could be a problem here?
askForPermission("android.permission.ACCESS_FINE_LOCATION");
This is what you had when passing into your method. Try doing :
askForPermission("Manifest.permission.ACCESS_FINE_LOCATION");
This may work. If not, I checked some other articles and everywhere I looked, it wasn't passed in as a String.
Try using this without a String:
(PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
Hope it works!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.