Reputation: 29
I created class GPSTracker.java in order to toast my current LatLng. set my permissions in manifest but isNetworkEnabled is still showing red in GPSTracker.java and throwing SecurityException: "network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission. Can anyone please tell me how to fix this. I have tried everything I can think of. Here is code for GPSTracker.java:
public class GPSTracker extends Service implements LocationListener{
private final Context context;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.context = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isGPSEnabled && !isNetworkEnabled) {
}
else {
this.canGetLocation = true;
if(isNetworkEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
}
if(locationManager !=null) {
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if(isGPSEnabled) {
if(location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if(locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
catch(Exception e) {
e.printStackTrace();
}
return location;
}
public void stopUsingGPS() {
if(locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude() {
if(location != null) {
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude() {
if(location != null) {
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation() {
return this.canGetLocation;
}
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("GPS is settings");
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public void onLocationChanged(Location arg0) {
}
@Override
public void onProviderDisabled(String arg0) {
}
@Override
public void onProviderEnabled(String arg0) {
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
And here is code for button on NewCatch.java
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
sqLiteHelper.insertData(
edtSpecies.getText().toString().trim(),
edtDate.getText().toString().trim(),
edtWeight.getText().toString().trim(),
edtLength.getText().toString().trim(),
edtSex.getText().toString().trim(),
edtBait.getText().toString().trim(),
edtMethod.getText().toString().trim(),
imageViewToByte(imageView)
);
Toast.makeText(getApplicationContext(), "Entry Added", Toast.LENGTH_LONG).show();
edtSpecies.setText("");
edtDate.setText("");
edtWeight.setText("");
edtLength.setText("");
edtSex.setText("");
edtBait.setText("");
edtMethod.setText("");
imageView.setImageResource(R.drawable.fishing);
}
catch(Exception e)
{
e.printStackTrace();
}
//////
gps = new GPSTracker(NewCatch.this);
if(gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Location Saved -\nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG);
}
else {
gps.showSettingsAlert();
}
//////
}
});
And last but not least my manifest:
<?xml version="1.0" encoding="utf-8"?>
<!--suppress AndroidDomInspection -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="devsolutionsbeyond.media.fishinglog">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<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=".Dash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewCatch" />
<activity android:name=".About" />
<activity android:name=".FishList" />
<activity android:name=".Maps"
android:label="@string/title_activity_maps" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.Fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<service android:name=".GPSTracker" />
</application>
Upvotes: 0
Views: 277
Reputation: 1235
Add this line in your code just inside service
boolean isNetworkEnabled= false;
And Add required permissions too for security exception related problem.
Upvotes: 0
Reputation: 5984
You check the permission and if not found, request one, like this:
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(LoginActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1600);
}
You can get the result as follows:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],@NonNull int[] grantResults) {
if(requestCode==REQUESTID){
// If request is cancelled, the result arrays are empty.
if (!(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
// permission was not granted, boo!
//show the permission rationale if the user did not click never show again
if(ActivityCompat.shouldShowRequestPermissionRationale(LoginActivity.this,android.Manifest.permission.ACCESS_FINE_LOCATION)){
//means user did not choose to never show permissions again. show the alert dialog
//Show a message explaining why the permission is necessary
//This explanation will only be shown if the user hasn't clicked do not show again on the permission dialog
}
}
}
}
And as always,enclose your location request call inside a try catch block because users can always just deny the permission which will cause crashes
try {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
} catch (SecurityException se) {
Toast.makeText(this, "Please provide location permissions to continue", Toast.LENGTH_SHORT).show();
}
Upvotes: 1
Reputation: 12735
In higher Android Versions (Android 6.0 (API level 23) or Above) some Permissions are labelled as dangerous!
Although you can mention them in Manifest and it will be enough for users with older Android Version but you will still have to ask users (For Higher Versions) at Runtime! And the System will ask the Users using a dialog. " (Your App Name) wants to access your Location " And the users will choose to Allow or Not and you will handle the Logic.
You can see the Android Documentation for this.
Upvotes: 1