简体   繁体   中英

How to stop the android application sending the pending location to webserver when i clicked on the logout button?

On logout button click, a flag is set to zero in my database through http post but i had implemented location listener in the same page (MainActivity.Java) so when a location is updated, it changes the flag value in database to 1. My Problem is even after i clicked on the logout button, some pending location values are passed to webserver and hence the flag is turned to 1. My question is, how to stop the locationlistner sending pending values after the "logout" button click?

below is my "MainActivity.java" implemented with LocationListner and Logout button also present in same class.

public class MainActivity extends Activity implements LocationListener
{


    LocationManager locationManager;
    String latitude;
    String longitude;
    String reverseString=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView txtLoc = (TextView) findViewById(R.id.textView1);
    final int pswd= SaveSharedPreference.getUserId(getBaseContext());
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

 try
{



     if (SaveSharedPreference.getVehiclenm(getBaseContext()).length()>0)
     {

         final String vname=SaveSharedPreference.getVehiclenm(getBaseContext());
//          Log.v("Set vname",""+vname);
            TextView setuser=(TextView) findViewById(R.id.vname);
            setuser.setText(vname);

     }

     if (SaveSharedPreference.getVhColor(getBaseContext()).length()>0)
     {

         final String vclr=SaveSharedPreference.getVhColor(getBaseContext());
//          Log.v("Set vcolor",""+vclr);
            TextView setvclr=(TextView) findViewById(R.id.vcolor);
            setvclr.setText(vclr);

     }

     if (SaveSharedPreference.getNumPlate(getBaseContext()).length()>0)
     {

         final String vplate=SaveSharedPreference.getNumPlate(getBaseContext());
//          Log.v("Set vplate",""+vplate);
            TextView setvplt=(TextView) findViewById(R.id.nplate);
            setvplt.setText(vplate);

     }

}
catch (Exception e) {
        Log.e("my error", e.toString());
    }





     Button lgt= (Button) findViewById(R.id.btn_lgt);
     lgt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub

                HttpClient httpclient = new DefaultHttpClient();

                HttpPost httppost = new HttpPost(ServerConnection.ip+"logout.jsp");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//              Log.v("pswd id for logout:",""+pswd);
                nameValuePairs.add(new BasicNameValuePair("password", ""+pswd));



            try {


                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String response = httpclient.execute(httppost, responseHandler);
                reverseString = response.trim();
//              Log.v("Response device id from logout",reverseString);


            }
             catch (Exception e) {


//              Log.e("logout error:",e.toString());
                runOnUiThread(new Runnable() 
                {                
                    @Override
                    public void run() 
                    {
                        if (AppStatus.getInstance(getBaseContext()).isOnline(getBaseContext())) {


                        Toast.makeText(getBaseContext(), "Logout Error", Toast.LENGTH_SHORT).show();

                        }

                     else {

                         Toast.makeText(getBaseContext(), "Connection Error", Toast.LENGTH_SHORT).show();
                    }
                    }   });


             }

                if(reverseString!=null){
                    SaveSharedPreference.clearUserDetails(getBaseContext());
                    Intent myIntent= new Intent (getBaseContext(),login.class);
                    startActivity(myIntent);
                    finish();
                }

            }
        }).start();



        }
        });
    }



    @Override
    public void onLocationChanged(Location location) {

        // TODO Auto-generated method stub
        TextView txtLoc = (TextView) findViewById(R.id.textView1);
        if(latitude==String.valueOf(location.getLatitude())&&longitude==String.valueOf(location.getLongitude()))
        {
            return;
        }
         latitude=String.valueOf(location.getLatitude());
         longitude=String.valueOf(location.getLongitude());




    final int driver_id=SaveSharedPreference.getUserId(getBaseContext());
         Thread thread = new Thread() {
              @Override
              public void run() {    
    //JSONObject j = new JSONObject();
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost(ServerConnection.ip+"updatedata.jsp");

    try {
        //j.put("lat",latitude);
        //j.put("lon", longitude);
        //Toast.makeText(getBaseContext(), ""+j.toString(), Toast.LENGTH_LONG).show();

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("lat", latitude));
        nameValuePairs.add(new BasicNameValuePair("lon", longitude));
        nameValuePairs.add(new BasicNameValuePair("password", ""+pswd));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);

        //This is the response from a jsp application
        String reverseString = response.trim();
//      Log.v("Response of password",reverseString);
        //Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        Log.e("jsonLocation 0:",e.toString());
    }
              }
              };
              thread.start();
//      Log.d("Test","test");
        //txtLat.setText(location.getLatitude().+location.getLongitude());

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
//      Log.d("Latitude", "disable");
        Toast.makeText(getBaseContext(), "Please turn on GPS", Toast.LENGTH_SHORT).show();
        Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
        startActivity(gpsOptionsIntent);
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
//      Log.d("Latitude","enable");


    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
//      Log.d("Latitude","status");

    }
        }

How to stop the locationlistner sending pending location values to webserver after i clicked the logout button?

Any piece of code is appreciated as am new to android and thanks in advance.

When you logout, you need to tell LocationManager that you no longer want updates. Something like --

locationManager.removeUpdates(MainActivity.this)

in your logout button onClickListener .

See LocationManager.removeUpdates

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM