Đây là class của mình, nhờ bạn xem hộ là mình có mắc sai lầm gì không:
package com.test;
// http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import java.util.Locale;
public class GPSTracker extends Service implements LocationListener
{
private final Context mContext;
private Boolean done = false;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
Long timeout = null;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 10 * 1; // 1 minute
protected LocationManager locationManager;
Callback successCallback;
Callback errorCallback;
public GPSTracker(Context context, Callback successCallback, Callback errorCallback, ReadableMap option)
{
this.mContext = context;
this.successCallback = successCallback;
this.errorCallback = errorCallback;
if (option.hasKey("timeout"))
this.timeout = new Long(option.getInt("timeout"));
this.getLocation();
}
private Thread thread = new Thread(new Runnable() {
public void run()
{
try
{
Thread.sleep(timeout);
synchronized (done)
{
if (done == true)
return;
WritableMap error = Arguments.createMap();
error.putString("message", "Hết thời gian.");
errorCallback.invoke(error);
done = true;
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
});
public void getLocation()
{
try
{
locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!isGPSEnabled)
{
// no network provider is enabled
}
else
{
this.canGetLocation = true;
if (isGPSEnabled && location == null)
{
synchronized (this.done)
{
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (this.timeout != null)
this.thread.start();
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void stopUsingGPS()
{
if(locationManager != null)
{
locationManager.removeUpdates(GPSTracker.this);
}
}
public boolean canGetLocation()
{
return this.canGetLocation;
}
public void showSettingsAlert()
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext.getApplicationContext());
String currentlang = Locale.getDefault().getLanguage();
boolean isVN = currentlang.equals(new Locale("vi").getLanguage());
// Setting Dialog Title
alertDialog.setTitle(isVN ? "Thông báo" : "Notice");
// Setting Dialog Message
alertDialog.setMessage(isVN ? "GPS hiện chưa được bật. Bạn có muốn vào menu thiết lập để bật không?"
: "GPS is not enabled. Do you want to go to settings menu?");
alertDialog.setPositiveButton(isVN ? "Thiết lập" : "Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.getApplicationContext().startActivity(intent);
}
});
alertDialog.setNegativeButton(isVN? "Bỏ qua" : "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
// Showing Alert Message
AlertDialog alert = alertDialog.create();
alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alert.show();
}
// event này không hề được gọi
@Override
public void onLocationChanged(Location location)
{
synchronized (this.done) {
if (this.done == true) return;
double lat = location.getLatitude();
double lng = location.getLongitude();
if (thread.isAlive())
thread.interrupt();
WritableMap pos = Arguments.createMap();
WritableMap coords = Arguments.createMap();
coords.putDouble("latitude", lat);
coords.putDouble("longitude", lng);
pos.putMap("coords", coords);
successCallback.invoke(pos);
this.done = true;
}
}
@Override
public void onProviderDisabled(String provider)
{
}
@Override
public void onProviderEnabled(String provider)
{
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
synchronized (this.done) {
if (this.done == true) return;
if (status == LocationProvider.OUT_OF_SERVICE ||
status == LocationProvider.TEMPORARILY_UNAVAILABLE) {
if (thread.isAlive())
thread.interrupt();
WritableMap error = Arguments.createMap();
error.putString("message", "Không lấy được gps");
errorCallback.invoke(error);
}
this.done = true;
}
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
}