Code will only return 0.0, 0.0 GPS coordinate while throwing NullPointerException
This tutorial seems like the most promising
It is truly awful code.
I can only seem to get this code to return a GPS coordinate of 0.0, 0.0 which isn't very useful
That is because it will only ever have a location if getLastKnownLocation()
happens to return one. It is unlikely to do so.
Rewritten but still not working permissions block in GPSTracker.java:
There are two problems here:
You cannot request permissions from a service.
This is not a real service. The author of this code elected to create a class that extends
Service
without actually implementing or using the service properly. This is what is causing yourNullPointerException
, as this is not a properly initializedContext
.
Can anyone please point me in the right direction here?
Throw all of this out.
FWIW, here is my sample app for using the LocationManager
API.
The recipe is fairly simple:
Step #1: Get a LocationManager
by calling getSystemService(Context.LOCATION_SERVICE)
on some Context
. I happen to do that in a fragment:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
template=getActivity().getString(R.string.url);
mgr=(LocationManager)getActivity()
.getSystemService(Context.LOCATION_SERVICE);
}
Step #2: Call requestUpdates()
, passing in something that implements LocationListener
. In my case, that happens to be the fragment itself:
@Override
@SuppressWarnings({"MissingPermission"})
public void onStart() {
super.onStart();
mgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3600000,
1000, this);
}
Step #3: In onLocationChanged()
of your LocationListener
, do something with the Location
that you get, which in my case is to execute an AsyncTask
to get a weather forecast:
@Override
public void onLocationChanged(Location location) {
new FetchForecastTask().execute(location);
}
Step #4: Call removeUpdates()
when you are done:
@Override
@SuppressWarnings({"MissingPermission"})
public void onStop() {
mgr.removeUpdates(this);
super.onStop();
}
And that's it (other than runtime permission stuff, which I abstract out into an AbstractPermissionsActivity
).
If you want, use getLastKnownLocation()
as an optimization, but do not rely upon it.
Latitude and Longitude showing as 0 in Android
Please manage required permission for marshmallow.
First you add this permission in manifest file
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
After in your activity First declare two variable like this,
private static final int REQUEST_CODE_PERMISSION = 1;
String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;
After in onCreate method
if(Build.VERSION.SDK_INT>= 23) {
if (checkSelfPermission(mPermission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{mPermission,
},
REQUEST_CODE_PERMISSION);
return;
}
else
{
*here manage your code if permission already access
}
}
Unable to get location from gps
Put this on your Manifest file before the application
tag:
<uses-permission android:name="android.permission.INTERNET" /> <!-- verify if connected to the internet -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- verify if connected to any network -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- verify location using GPS, give precise location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- verify location using WiFi and mobile, gives approximate location -->
Related Topics
Java - Declaring from Interface Type Instead of Class
How to Solve the "Double-Checked Locking Is Broken" Declaration in Java
Mapping Manytomany with Composite Primary Key and Annotation:
Java Interfaces/Implementation Naming Convention
Creating Runnable Jar with Gradle
How Apply CSS on a <H:Inputtext>
How Does Facebook Add Badge Numbers on App Icon in Android
How to Create Android Virtual Device
Android: Switch Camera When Button Clicked
Dynamic Listview in Android App
Passing a JavaScript Object Using Addjavascriptinterface() on Android
Creating Hashmap from a JSON String
Dx Bad Class File Magic (Cafebabe) or Version (0033.0000) with Adk14
Android: How to Handle Button Click
Android Change Material Elevation Shadow Color
Android - Copy Assets to Internal Storage
How to Create Your Own Library for Android Development to Be Used in Every Program You Write