簡體   English   中英

當我單擊注銷按鈕時,如何停止android應用程序將待處理位置發送到Web服務器?

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

單擊注銷按鈕時,通過http發布在數據庫中將標志設置為零,但是我在同一頁面(MainActivity.Java)中實現了位置偵聽器,因此當位置更新時,它將數據庫中的標志值更改為1。問題是什至在我單擊注銷按鈕后,一些未決的位置值也傳遞到了Web服務器,因此該標志變為1。我的問題是,如何在單擊“注銷”按鈕后停止locationlistner發送未決的值?

下面是我的“ MainActivity.java”,它通過LocationListner和Logout按鈕實現,也存在於同一類中。

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");

    }
        }

單擊注銷按鈕后,如何停止locationlistner向Web服務器發送待處理的位置值?

任何一段代碼都被視為Android的新功能,在此先感謝您。

注銷時,您需要告訴LocationManager您不再需要更新。 就像是 -

locationManager.removeUpdates(MainActivity.this)

在您的注銷按鈕onClickListener

請參閱LocationManager.removeUpdates

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM