2017-06-02 4 views
-2

Hier verwende ich Ort, um Länge und Breite zu aktualisieren, immer wenn ich versuche, Taste oder App Hintergrund oder eine Instanz zu drücken, ich möchte, dass die location_thread ausgeführt wird. Setze es fort, wenn ich zur aktuellen Aktivität wechsele.Wie man Thread in onBackPressed oder OnPause oder OnResume stoppt

location_thread = new Thread() { 

      @Override 
      public void run() { 
       try { 
        while (!isInterrupted()) { 
         Thread.sleep(2000); 
         runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           Log.i("TAG",currentThread().getName()+" Running"); 
           updateLatLongInfo(); 
          } 
         }); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }; 
     location_thread.setDaemon(false); 
     location_thread.start(); 
    } 

    @Override 
//whenever user press a backbutton thread must stoppped 
    public void onBackPressed() { 
     super.onBackPressed(); 
     location_thread.stop(); 
     Intent intent = new Intent(Logistic_ReportProblem.this, FPAgentHome.class); 
     startActivity(intent); 
     finish(); 
    } 


    private void updateLatLongInfo() { 
     try { 
      int locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE); 
      latlongInfo.setBackgroundResource(R.drawable.latlng_info); 
      GradientDrawable drawable = (GradientDrawable) latlongInfo.getBackground(); 
      if (locationMode == 0 || locationMode == 1 || locationMode == 2) { 
       latlongInfo.setText("Please enable high accuracy in your location settings"); 
       drawable.setColor(Color.RED); 
      } else { 
       try { 
        String context = Context.LOCATION_SERVICE; 
        locationManager = (LocationManager) this.getSystemService(context); 
        String provider = LocationManager.GPS_PROVIDER; 
        android.location.Location location = locationManager.getLastKnownLocation(provider); 
        updateWithNewLocation(location); 
        locationManager.requestLocationUpdates(provider, 0, 0, locationListener); 

        param_latitude = df.format(lat).toString(); 
        param_longitude = df.format(lng).toString(); 

        // latlongInfo.setText("Latitude :" + Math.round(lat * 1000000.0)/1000000.0 + " ," + " Longitude :" + Math.round(lng * 1000000.0)/1000000.0); 
        latlongInfo.setText("Latitude : " + param_latitude + " ," + " Longitude : " + param_longitude); 
        latlongInfo.setGravity(Gravity.CENTER); 
        drawable.setColor(Color.GREEN); 
       } catch (SecurityException e) { 
        Log.e("Error", e.toString()); 
       } 
      } 
+0

Das Hauptproblem ist, dass Sie "getLastKnownLocation" für die Aktualisierung des Standorts verwenden ... es ist eine schreckliche Idee ... statt dies sollten Sie dies mit 'locationListener' tun ... Sie können Beispielcode auf offiziellen Android finden Leitfaden Website – Selvin

Antwort

0

Dont Thread.stop() Methode verwenden, seine deprecated and unsafe

Am besten Weg Thread zu stoppen, ist es `s

erstellen flüchtige Variable abzuschließen arbeiten, so etwas wie

public volatile boolean threadIsStopped; 

Ersetzen Sie Ihre

while (!isInterrupted()) 

zu

while (!threadIsStopped) 

Und dann ist es nur wahr machen

public void onBackPressed() { 
    super.onBackPressed(); 
    threadIsStopped = true; 
    Intent intent = new Intent(Logistic_ReportProblem.this, FPAgentHome.class); 
    startActivity(intent); 
    finish(); 
} 

Da Ihr backpress eine andere Tätigkeit beginnt und endet Strom seine besser so etwas zu tun:

@Override 
protected void onResume() { 
    super.onResume(); 
    threadIsStopped = false; 
    //start thread here 
} 

@Override 
protected void onPause() { 
    super.onResume(); 
    threadIsStopped = true; 
} 

So gibt s no need to stop thread in onBackPressed überhaupt.

Beachten Sie, dass es eine mögliche Verzögerung gibt, nachdem threadIsStopped festgelegt wurde, bevor der Thread gestoppt wird.

Über das Fortsetzen des Threads - in Ihrem Fall erstellen Sie einfach einen neuen Thread und starten ihn. Bitte beachten Sie, dass und onResume erneut aufgerufen werden, wenn Sie die Ausrichtung Ihres Geräts ändern. Deshalb wird dringend empfohlen, IntentService für solche Dinge zu verwenden.

Verwandte Themen