我正在使用谷歌地图显示(固定)不同的地方。点击后退按钮后显示不同的地方后,它将重定向到我的主屏幕。大约10-15秒后,我的应用程序关闭了。我相信位置更新仍在导致此错误的后台。如何停止在后台更新活动?

    package com.bar.start;

    import java.util.ArrayList;
    import org.apache.http.HttpEntity;
    import org.apache.http.conn.ClientConnectionManager;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
    import org.apache.http.params.HttpParams;
    import com.bar.barapp.R;
    import com.bar.location.Loc;
    import com.bar.location.Webcall;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.MapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.Circle;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.print.PrintAttributes;
    import android.util.Log;
    import android.widget.ArrayAdapter;

    public class LocationActivity extends Activity implements LocationListener {

     private final String TAG = getClass().getSimpleName();
     protected ConnectionDetector connectionDetector;
     public Boolean isInternetPresent;
     private String[] places;
     public static Loc [] getloc;
     private LocationManager locationManager;
     private Location loc;

     static HttpEntity entityResponse = null;
     GoogleMap map;
     MarkerOptions mp;
     Activity activity;
     Context context;
     LocationListener listener;
     Double lat,lon;
    final   String Googlekey="APikey" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {



        SharedPreferences sharedPreferences;
        int locationCount = 0;
        connectionDetector = new ConnectionDetector(getApplicationContext());
        isInternetPresent = connectionDetector.isConnectingToInternet();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);

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

      lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);

      map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    }

    @Override
    public void onLocationChanged(Location location) {

        map.clear();
        lat=location.getLatitude();
        lon=location.getLongitude();

        Googleapicall exeTask = new Googleapicall();
          exeTask.execute();

          mp = new MarkerOptions();

           mp.position(new LatLng(lat, lon));
           map.addMarker(mp);
           mp.title("My Location");
   p.animateCamera(CameraUpdateFactory.newLatLngZoom(
          new LatLng(lat, lon), 13));

    }

    @Override
    public void onProviderDisabled(String provider) {


    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }



    public class Googleapicall extends AsyncTask<String, Void, String>

    {


        Loc statStr = null;
         Loc[] statInfoResult = null;
         ArrayList<Loc> SIArrayList = new ArrayList<Loc>();

        private ProgressDialog Dialog;

        @Override
        protected void onPreExecute() {
            Dialog = new ProgressDialog(LocationActivity.this);
            Dialog.setMessage(LocationActivity.this.getResources().getString(
                    R.string.loading));
            Dialog.setCancelable(false);
            Dialog.show();

        }

        @Override
        protected String doInBackground(String... params) {

            String result = "";
            try
            {

                if(isInternetPresent)

                {
                getloc=Webcall.getdet(lat,lon,Googlekey);
                result = "Success";
                System.out.println("getloc "+getloc.length);

                }


                else
                {
                    result = "Failure";
                }
            }


            catch (Exception e)
            {
                result = "Failure";
                e.printStackTrace();
            }

            return result;


        }

        @Override
        protected void onPostExecute(String result) {


            Loc lc=new Loc();

            try {

                if(result.contains("Success"))
                {

                    mp=new MarkerOptions();

                    for(int i=0;i<getloc.length;i++)
                    {
                        Double lati=getloc[i].latitudes;
                        Double longi=getloc[i].longitudes;
                           map.addMarker(new MarkerOptions()
                              .title("Nearbyplace")
                              .position(
                                new LatLng(lati,longi))
                              .icon(BitmapDescriptorFactory.fromResource(R.drawable.small)));
                    }
                }

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

            }

            Dialog.dismiss();

        }

    }


}


04-06 11:09:53.768: E/AndroidRuntime(5188):     FATAL EXCEPTION: main
04-06 11:09:53.768: E/AndroidRuntime(5188):     android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@42ea1380 is not valid; is your activity running?
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:796)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.app.Dialog.show(Dialog.java:287)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.bar.start.LocationActivity$Googleapicall.onPreExecute(LocationActivity.java:194)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.AsyncTask.execute(AsyncTask.java:534)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.bar.start.LocationActivity.onLocationChanged(LocationActivity.java:104)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.Looper.loop(Looper.java:137)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.app.ActivityThread.main(ActivityThread.java:5419)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at java.lang.reflect.Method.invoke(Method.java:525)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at dalvik.system.NativeStart.main(Native Method)

最佳答案

全局声明此值。

int i=0;


在上面的代码中进行以下更改。因此它将仅更新一次。(windowmanager badtokenexception-无法添加窗口)此异常将不会再次发生。 ,别客气。

@Override
        public void onLocationChanged(Location location) {


               for(;i<=0;i++)
               {

                   map.clear();

                    lat=location.getLatitude();
                    lon=location.getLongitude();

                      mp = new MarkerOptions();

                       mp.position(new LatLng(lat, lon));
                       map.addMarker(mp);
                       mp.title("My Location");

                   map.animateCamera(CameraUpdateFactory.newLatLngZoom(
                              new LatLng(lat, lon), 13));

                            Googleapicall exeTask = new Googleapicall();
                              exeTask.execute();

               }


        }

07-27 18:06