Java 请求服务内部的位置权限
我正在使用服务获取位置数据,此服务在我的应用程序在活动上运行时启动,我需要该活动来请求权限 所以基本上这是我的服务(效果很好) 我从这里开始Java 请求服务内部的位置权限,java,android,google-maps,location,fusedlocationproviderapi,Java,Android,Google Maps,Location,Fusedlocationproviderapi,我正在使用服务获取位置数据,此服务在我的应用程序在活动上运行时启动,我需要该活动来请求权限 所以基本上这是我的服务(效果很好) 我从这里开始 package com.example.afcosta.inesctec.pt.android; import android.Manifest; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; i
package com.example.afcosta.inesctec.pt.android;
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.example.afcosta.inesctec.pt.android.services.GoogleLocation;
public class LocationPermission extends AppCompatActivity {
public static final int REQ_PERMISSION = 99;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
askPermission();
Intent i = new Intent(this,Login.class);
startActivity(i);
}
// PEDIDO DE PERMISSÃO
private void askPermission() {
checkLocationPermission();
}
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission. ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission. ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new AlertDialog.Builder(this)
.setTitle("Partilhar localização")
.setMessage("Permitir a partilha de dados sobre a sua localização?")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(LocationPermission.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
})
.create()
.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission. ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
startService(new Intent(this, GoogleLocation.class));
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission. ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
startService(new Intent(this, GoogleLocation.class));
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
}
}
}
问题是,当他第一次运行应用程序时,我需要请求权限,或者他禁用了位置。这永远不会发生
我应该在用户授予权限后启动服务
有什么帮助吗
谢谢您可以通过以下方式在运行时请求任何权限:
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, RC_ACCESS_FINE_LOCATION);
}
}
您可以通过以下方式获得结果:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case RC_ACCESS_FINE_LOCATION: {
}
}
}
应在启动服务之前请求许可
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case RC_ACCESS_FINE_LOCATION: {
}
}
}
@Override
protected Boolean processInBackground(Void... params) {
PermissionResponse response = PermissionEverywhere.getPermission(getApplicationContext(),
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQ_CODE,
"Notification title",
"This app need external permisison",
R.launcher)
.call();
boolean isGranted = response.isGranted();
if(isGrante){
// here do your stuff
}
}