簡體   English   中英

使用stopService()后GPS服務仍在運行

[英]GPS service still running after using stopService()

我的應用程序有一個后台服務,該服務在一定時間間隔內處理位置更新。 我試圖停止該服務以不同的間隔啟動一個新服務,但是最終發生的是該應用程序以不同的間隔發送了兩個位置更新。 基本上,使用stopService()后,我的第一個服務不會停止。

這是啟動服務的代碼:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_activity_f);

        tiempoGPS = 25000;
        startService(new Intent(getBaseContext(), ServicioDeFondo.class).putExtra("TiempoGPS", Integer.toString(tiempoGPS)));

    }

這是單擊按鈕后我將其停止的部分:

 private void selectItem(int position) {

...

        if(menus[position].equals("Configuracion"))
        {
            try {

                stopService(new Intent(getBaseContext(),ServicioDeFondo.class));

            }
            catch(Exception e)
            {
                ...
            }
        }

}

這是我的Service類的onCreate方法:

 @Override
    public void onCreate()
    {
        mHandler = new Handler();
        TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        telefonoID = telephonyManager.getDeviceId();
        telephonyManager = null;

        createLocationRequest();
        buildGoogleApiClient();
        mGoogleApiClient.connect();
    }

OnstartCommand方法:

@Override
    public int onStartCommand(Intent intent, int flags, int startId)
     {
         tiempoEntreActualizacion = Integer.parseInt((String)  intent.getExtras().get("TiempoGPS"));

        reportarGPS = new Thread(new Runnable() { public void run()
        {
            try
            {
                while(true)
                {
                    try {
                        Thread.sleep(tiempoEntreActualizacion);
                        new APISendClass().execute();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        mGoogleApiClient.disconnect();
                        Thread.currentThread().interrupt();
                        return;
                    }
                }
            }
         catch (Exception e)
        {
            //TODO Auto-generated catch block
            e.printStackTrace();
        }
        } });

        reportarGPS.start();

        return START_NOT_STICKY;
    }

這是我的服務類onDestroy方法:

@Override
    public void onDestroy(){
    reportarGPS.interrupt();
    reportarGPS = null;
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this);
        mGoogleApiClient.disconnect();

    mGoogleApiClient = null;
    mHandler.removeCallbacksAndMessages(null);
    Thread.currentThread().interrupt();
    super.onDestroy();
}

最后但並非最不重要的一點是,我啟動locationUpdates的方法:

@Override
    public void onConnected(Bundle bundle) {
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
        if (mLastLocation != null) {
            latitude =String.valueOf(mLastLocation.getLatitude());
            longitude = String.valueOf(mLastLocation.getLongitude());
        }
        startLocationUpdates();
    }


    protected void startLocationUpdates() {
        LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);
    }

現在已經堅持了2天。 我確定它是一些非常基本的錯誤,但是我無法指出,我的onDestroy方法已被調用,但是即使在運行它之后,該位置仍在更新中。 為什么?? 如何完全取消服務?

您的線程是錯誤的。 如果它被打斷,它永遠不會發出煩惱。 中斷線程不會停止它,它會設置一個可由線程本身檢查以查看其是否完成的標志。 (出於各種原因,在任何地方都停止線程是一個非常糟糕的主意)。 使您的while循環在線程中檢查isInterrupted,如果為true,則從其run函數返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM