mirror of
https://github.com/apache/cordova-android.git
synced 2025-03-04 00:13:20 +08:00
Fixed the Geolocation Functionality. If it is able to get the location, it will get it from the
GPS first, then the network.
This commit is contained in:
parent
337acc92ea
commit
ceb2dd61c2
73
src/com/nitobi/droidgap/GpsListener.java
Normal file
73
src/com/nitobi/droidgap/GpsListener.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package com.nitobi.droidgap;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.hardware.SensorManager;
|
||||||
|
import android.location.Location;
|
||||||
|
import android.location.LocationManager;
|
||||||
|
import android.location.LocationListener;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Vibrator;
|
||||||
|
import android.telephony.TelephonyManager;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.webkit.WebView;
|
||||||
|
|
||||||
|
public class GpsListener implements LocationListener {
|
||||||
|
|
||||||
|
private Context mCtx;
|
||||||
|
private Location cLoc;
|
||||||
|
private LocationManager mLocMan;
|
||||||
|
private static final String LOG_TAG = "PhoneGap";
|
||||||
|
|
||||||
|
public GpsListener(Context ctx)
|
||||||
|
{
|
||||||
|
mCtx = ctx;
|
||||||
|
mLocMan = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE);
|
||||||
|
mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
|
||||||
|
cLoc = mLocMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Location getLocation()
|
||||||
|
{
|
||||||
|
return cLoc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onProviderDisabled(String provider) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
Log.d(LOG_TAG, "The provider " + provider + " is disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onProviderEnabled(String provider) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
Log.d(LOG_TAG, "The provider "+ provider + " is enabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
Log.d(LOG_TAG, "The status of the provider " + provider + " has changed");
|
||||||
|
if(status == 0)
|
||||||
|
{
|
||||||
|
Log.d(LOG_TAG, provider + " is OUT OF SERVICE");
|
||||||
|
}
|
||||||
|
else if(status == 1)
|
||||||
|
{
|
||||||
|
Log.d(LOG_TAG, provider + " is TEMPORARILY_UNAVAILABLE");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.d(LOG_TAG, provider + " is Available");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void onLocationChanged(Location location) {
|
||||||
|
Log.d(LOG_TAG, "The location has been updated!");
|
||||||
|
cLoc = location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasLocation() {
|
||||||
|
return (cLoc != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
69
src/com/nitobi/droidgap/NetworkListener.java
Normal file
69
src/com/nitobi/droidgap/NetworkListener.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package com.nitobi.droidgap;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.hardware.SensorManager;
|
||||||
|
import android.location.Location;
|
||||||
|
import android.location.LocationManager;
|
||||||
|
import android.location.LocationListener;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Vibrator;
|
||||||
|
import android.telephony.TelephonyManager;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.webkit.WebView;
|
||||||
|
|
||||||
|
public class NetworkListener implements LocationListener {
|
||||||
|
|
||||||
|
private Context mCtx;
|
||||||
|
private Location cLoc;
|
||||||
|
private LocationManager mLocMan;
|
||||||
|
private static final String LOG_TAG = "PhoneGap";
|
||||||
|
|
||||||
|
public NetworkListener(Context ctx)
|
||||||
|
{
|
||||||
|
mCtx = ctx;
|
||||||
|
mLocMan = (LocationManager) mCtx.getSystemService(Context.LOCATION_SERVICE);
|
||||||
|
mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, this);
|
||||||
|
cLoc = mLocMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Location getLocation()
|
||||||
|
{
|
||||||
|
return cLoc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onProviderDisabled(String provider) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
Log.d(LOG_TAG, "The provider " + provider + " is disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void onProviderEnabled(String provider) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
Log.d(LOG_TAG, "The provider "+ provider + " is enabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
Log.d(LOG_TAG, "The status of the provider " + provider + " has changed");
|
||||||
|
if(status == 0)
|
||||||
|
{
|
||||||
|
Log.d(LOG_TAG, provider + " is OUT OF SERVICE");
|
||||||
|
}
|
||||||
|
else if(status == 1)
|
||||||
|
{
|
||||||
|
Log.d(LOG_TAG, provider + " is TEMPORARILY_UNAVAILABLE");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Log.d(LOG_TAG, provider + " is Available");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void onLocationChanged(Location location) {
|
||||||
|
Log.d(LOG_TAG, "The location has been updated!");
|
||||||
|
cLoc = location;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user