0

我的应用程序有一点问题。我从 GPS 或网络获取位置,一切正常,但...

如果我从 GPS 获取位置并且当我没有从网络获取位置时我从手机关闭 GPS 所以.. 我需要帮助,

当我在网络上关闭 GPS 开关时如何制作

我的代码:

public class Background_work extends Service implements LocationListener{

    static String TAG="Service Created";
    private Timer timer = new Timer();
    private TimerTask task;
    int count = 0;
    LocationManager lm;
    // Platuma ilguma
    double lat,log;
    //Tikrina kas ijungta
    boolean GPSStatus = false;
    boolean NetworkStatus = false;

    public void setX(double X){
        lat = X;
    }
    public void setY(double Y){
        log = Y;
    }
    public double getX(){
        return lat;
    }
    public double getY(){
        return log;
    }

    public int onStartComand(Intent intent, int flags, int startId){
        Toast.makeText(this, "Setver Started", Toast.LENGTH_LONG).show();
        return Service.START_NOT_STICKY;
    }

    public void onCreate() {
        super.onCreate();
        Log.d(Background_work.TAG, "Service Created");
    }

    @Override
    public void onStart(Intent intent, int startId) {   

    Log.d(Background_work.TAG, "Service started");

    subscribeToLocationUpdates();

}
       @Override
       public void onDestroy() {
           super.onDestroy();
       }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onLocationChanged(Location location) {

        double latitude = location.getLatitude();
        double longitude = location.getLongitude();

        if(location != null){

        setX(latitude);
        setY(longitude);

        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    public void subscribeToLocationUpdates() {

        this.lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

        // GPS status
        GPSStatus = this.lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // Network status
        NetworkStatus = this.lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        Log.i("", "GPS" + GPSStatus + " network" + NetworkStatus);


        if(GPSStatus == true){
            this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
        }else if(NetworkStatus == true){
            this.lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, this);
        }else if(NetworkStatus == true && GPSStatus == true){
            this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, this);
        }

    }

}
4

0 回答 0