0

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>

  • https://stackoverflow.com/questions/43082062/code-will-only-return-0-0-0-0-gps-coordinate-while-throwing-nullpointerexceptio – CommonsWare Dec 22 '17 at 22:38
  • First off, you need to ask for runtime permission. Secondly- this class is hideously broken and should never be used. See http://gabesechansoftware.com/location-tracking/ for why its broken and better solutions. – Gabe Sechan Dec 22 '17 at 22:40
  • Possible duplicate of [Request Permission at Runtime for Android Marshmallow 6.0](https://stackoverflow.com/questions/34150083/request-permission-at-runtime-for-android-marshmallow-6-0) – Tomin B Azhakathu Dec 23 '17 at 04:45
  • isNetworkEnabled(); is a custom function which return boolean value true or false. make your function to check mobile data or wifi is on or off and return true false and accordingly and then perform youe Action. – Shubham Dec 23 '17 at 04:58

3 Answers3

1

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.

Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • Okay I have edited but I would like first if you try running your app in an older android device @Devsolutionsbeyond if you can access? – Xenolion Dec 22 '17 at 22:43
  • I want to make sure this is the only one error in your program before solving it! – Xenolion Dec 22 '17 at 22:44
  • also isNetworkEnabled cannot resolve symbol...sry for the ignorance – Devsolutionsbeyond Dec 22 '17 at 22:44
  • same thing in nougat and marshmellow – Devsolutionsbeyond Dec 22 '17 at 22:48
  • Marshamallow and Nougat are both 23 and above! You need Lollipop or less! – Xenolion Dec 22 '17 at 22:49
  • Oooh there are some other errors in your app then, not only this! Okay here is a piece of advice, It seems like you get this code from some old tutorial in the Internet! While Android API is always updated after some time! So I am sorry to say you may also pass through a number of errors or struggles till you finish. So always prefer the android documentation trainings themselves and see this question and answer, here the code real appear like yours(a little modified) check the first answer by Commonsware(Our best in Android Stack Overflow). Check the link in the next comment. – Xenolion Dec 22 '17 at 23:03
  • https://stackoverflow.com/questions/43082062/code-will-only-return-0-0-0-0-gps-coordinate-while-throwing-nullpointerexceptio – Xenolion Dec 22 '17 at 23:03
  • @Devsolutionsbeyond i shared a sample which i use, you can refer it – Kushan Dec 22 '17 at 23:20
1

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();
                }
Kushan
  • 5,855
  • 3
  • 31
  • 45
  • Here is have only used the fine location, you add the coarse location to the list and check inside the result too instead of the hard coded 0 index that i have used – Kushan Dec 22 '17 at 23:08
0

Add this line in your code just inside service

boolean isNetworkEnabled= false;

And Add required permissions too for security exception related problem.

Shubham
  • 1,205
  • 1
  • 12
  • 15