在背景中活动时敬酒(当我按下主页按钮时)

时间:2016-03-11 11:35:08

标签: android android-toast

我正在使用此代码在我的活动中显示toast在前台: -

@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();
}

但是在Logs中,即使活动处于后台并正确打印,我也会打印位置更改: -

 /**
 * 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)");
    }
}

我希望即使活动在后台也能显示吐司。怎么做到这一点?

3 个答案:

答案 0 :(得分:0)

获取上下文并尝试:

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

答案 1 :(得分:0)

尝试此方法,创建应用程序的静态变量并使用它。

像,

public class YourApplication extends Application {

    public static Application instance=null;

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

并将您的方法更改为

@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(); 
}

答案 2 :(得分:0)

试试这个:

runOnUiThread(new Runnable() {

    public void run() {

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