0
I have a project that when accessing Activity, it already searches the place and fills the data, to save them, later. But when it is the first access, when searching for permissions, he always understands that the permission for GPS has not been given.
Follows code:
private void startLocationUpdates(){
//Inicia GPS para buscar o local atual
if (!edtLatitude.getText().toString().isEmpty() || !edtLongitude.getText().toString().isEmpty())
return;
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
showAlertOk("Permissão para acessar GPS não foi concedida, portanto não será possível buscar o endereço atual automaticamente.");
}else {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(2000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
showProgress(true);
}
}
So I’d like to understand where I’m going wrong, because whenever it’s the first access, it falls on this alert and doesn’t call the place. When permission is already given, it works. Usually.
EDIT: Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="projeto.projeto1_teste">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Allows the API to use WiFi or mobile cell data (or both) to determine the device's location. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Allows the API to use the Global Positioning System (GPS) to determine the device's location to within a very small area. -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="APIGOOGLE"/>
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
EDIT2:
Runtime permissions for android 6.0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Solicita as permissões
String[] permissoes = new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
};
Permissions.validate(this, 0, permissoes);
Permissions class:
public class Permissions {
/**
* Solicita as permissões
*/
public static boolean validate(Activity activity, int requestCode, String... permissions) {
List<String> list = new ArrayList<String>();
for (String permission : permissions) {
// Valida permissão
boolean ok = ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED;
if (!ok) {
list.add(permission);
}
}
if (list.isEmpty()) {
// Tudo ok, retorna true
return true;
}
// Lista de permissões que falta acesso.
String[] newPermissions = new String[list.size()];
list.toArray(newPermissions);
// Solicita permissão
ActivityCompat.requestPermissions(activity, newPermissions, 1);
return false;
}
}
Please avoid long discussions in the comments; your talk was moved to the chat
– Maniero