java - 当我单击注销按钮时,如何阻止 Android 应用程序将挂起位置发送到网络服务器?

标签 java android http-post runnable locationlistener

单击注销按钮时,通过 http post 在我的数据库中将标志设置为零,但我在同一页面 (MainActivity.Java) 中实现了位置监听器,因此当更新位置时,它将数据库中的标志值更改为1. 我的问题是,即使在我单击注销按钮后,一些挂起的位置值也会传递到网络服务器,因此标志会变为 1。我的问题是,如何在单击“注销”按钮后停止位置列表器发送挂起的值?

下面是我使用 LocationListner 实现的“MainActivity.java”,注销按钮也存在于同一类中。

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

    }
        }

单击注销按钮后,如何阻止位置列表器向网络服务器发送挂起的位置值?

任何一段代码都会受到赞赏,因为我是 Android 新手,并提前致谢。

最佳答案

当您注销时,您需要告诉LocationManager您不再需要更新。类似的东西——

locationManager.removeUpdates(MainActivity.this)

在注销按钮中onClickListener

参见LocationManager.removeUpdates

关于java - 当我单击注销按钮时,如何阻止 Android 应用程序将挂起位置发送到网络服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569534/

相关文章:

java - 异步post方法中设置连接超时的方法

java - 为异构键编写 hashCode 方法

java - libGDX - 如果我播放声音,我的 Android 手机会口吃/有微延迟

Java 快捷方式 : generating an array to change a for loop into a for-each loop

android - Okhttp Authenticator 多线程

android - 图库 View 中图像的滚动速度

android - 如何将表格布局作为子项添加到 ExpandableListView。

css - 在 IE7 中将鼠标悬停在背景图片上会停止加载指示

JavaFX 对话框一次又一次返回相同的结果

go - 如何从 itop REST API 获取 UserRequest 数据