2016-03-11 1 views
0

J'utilise ce code pour afficher des toasts dans mon activité quand il est au premier plan: -Toast lorsque l'activité en arrière-plan (quand je presse le bouton d'accueil)

@Override 
public void onLocationChanged(Location location) { 
    // Assign the new location 
    mLastLocation = location; 

    Toast.makeText(getApplicationContext(), "Location changed!", 
      Toast.LENGTH_SHORT).show(); 

    // Displaying the new location on UI 
    displayLocation(); 
} 

Mais dans les journaux, je suis en train d'imprimer les changements d'emplacement même lorsque l'activité est en arrière-plan et ils s'imprimer correctement: -

/** 
* Method to display the location on UI 
* */ 
private void displayLocation() { 

    mLastLocation = LocationServices.FusedLocationApi 
      .getLastLocation(mGoogleApiClient); 

    if (mLastLocation != null) { 
     double latitude = mLastLocation.getLatitude(); 
     double longitude = mLastLocation.getLongitude(); 
     //getting previous location from text view if location updates are not just started 
     Log.d(TAG,"mLocationUpdatesJustStarted = "+mLocationUpdatesJustStarted); 
     if(!mLocationUpdatesJustStarted){ 
      String previousLocation = lblLocation.getText().toString(); 
      double previousLatitude, previousLongitude; 
      if(previousLocation!=null && !previousLocation.isEmpty()){ 
       int index = previousLocation.indexOf(','); 
       if(index!=-1){  //In the error printed in below else block there is no comma, so parsing error will not occur 
        previousLatitude = Double.parseDouble(previousLocation.substring(0,index)); 
        previousLongitude = Double.parseDouble(previousLocation.substring(index+2)); //one space is there 
        Log.d(TAG, "Calculating distance between (" + previousLatitude + ", " + previousLongitude + ") and (" + latitude + "," + longitude + ") in metres."); 
        //updating distance travelled 
        double distanceFormula, distanceLoc, distanceRad; 
        distanceFormula = distance(previousLatitude,previousLongitude,latitude,longitude,"m"); 
        distanceLoc = distanceLocationAPI(previousLatitude,previousLongitude,latitude,longitude,"m"); 
        distanceRad = distanceUsingRadius(previousLatitude,previousLongitude,latitude,longitude,"m"); 
        Log.d(TAG,"The three distances are : "+distanceFormula+", "+distanceLoc+", "+distanceRad); 
        DISTANCE_TRAVELLED += distanceFormula; 
        DISTANCE_TRAVELLED_LOCATION_API += distanceLoc; 
        DISTANCE_TRAVELLED_RADIUS += distanceRad; 
       } 
      } 
     } 

     //update location updates just started flag 
     if(mLocationUpdatesJustStarted) 
      mLocationUpdatesJustStarted = false; 

     lblLocation.setText(latitude + ", " + longitude); 
     distTravelled.setText("Distance Travelled (as per geolocation) = "+DISTANCE_TRAVELLED +" mtrs"+ 
       "\nDistance Travelled (as per Location) = "+DISTANCE_TRAVELLED_LOCATION_API +" mtrs"+ 
       "\nDistance Travelled (using earth's radius) = "+DISTANCE_TRAVELLED_RADIUS +" mtrs"); 

    } else { 

     lblLocation 
       .setText("(Couldn't get the location. Make sure location is enabled on the device)"); 
    } 
} 

Je veux que le toast à montrer, même si l'activité est en arrière-plan. Comment y parvenir?

+0

Je suppose que cela ne se produira pas d'activité. Vous devez utiliser le service. –

Répondre

0

Obtenez le contexte et essayez ceci:

context.runOnUiThread(new Runnable() 
{ 
    public void run() 
    { 
     Toast.makeText(getApplicationContext(), "Location changed!", 
     Toast.LENGTH_SHORT).show();    
    } 
}); 
0

Essayez cette méthode, créez une variable statique de votre application et l'utiliser.

Comme,

public class YourApplication extends Application { 

    public static Application instance=null; 

    protected void onCreate(Bundle bundle) { 
     this.instance=this; 
    } 
} 

et changer votre méthode,

@Override 
public void onLocationChanged(Location location) { 
    // Assign the new location 
    mLastLocation = location; 

    Toast.makeText(YourApplication.instance, "Location changed!", 
     Toast.LENGTH_SHORT).show(); 

    // Displaying the new location on UI 
    displayLocation(); 
} 
0

Essayez ceci:

runOnUiThread(new Runnable() { 

    public void run() { 

     Toast.makeText(getApplicationContext(), "Toast Msg Here", Toast.LENGTH_SHORT).show(); 
    } 
});