Java 当我单击注销按钮时,如何停止android应用程序将挂起的位置发送到Web服务器?
单击注销按钮时,通过http post在我的数据库中将标志设置为零,但我在同一页面(MainActivity.Java)中实现了位置侦听器,因此当更新位置时,它会将数据库中的标志值更改为1。 我的问题是,即使在我单击注销按钮之后,一些挂起的位置值也会传递给webserver,因此标志变为1。我的问题是,如何在单击“注销”按钮后停止locationlistner发送挂起的值 下面是我用LocationListner和Logout按钮实现的“MainActivity.java”,它们也出现在同一个类中Java 当我单击注销按钮时,如何停止android应用程序将挂起的位置发送到Web服务器?,java,android,http-post,runnable,locationlistener,Java,Android,Http Post,Runnable,Locationlistener,单击注销按钮时,通过http post在我的数据库中将标志设置为零,但我在同一页面(MainActivity.Java)中实现了位置侦听器,因此当更新位置时,它会将数据库中的标志值更改为1。 我的问题是,即使在我单击注销按钮之后,一些挂起的位置值也会传递给webserver,因此标志变为1。我的问题是,如何在单击“注销”按钮后停止locationlistner发送挂起的值 下面是我用LocationListner和Logout按钮实现的“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");
}
}
public类MainActivity扩展活动实现LocationListener
{
地点经理地点经理;
弦纬度;
弦经度;
字符串反向限制=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtLoc=(TextView)findViewById(R.id.textView1);
final int-pswd=saveSharedReference.getUserId(getBaseContext());
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
locationManager.RequestLocationUpdate(locationManager.GPS\提供程序,0,0,此);
尝试
{
if(saveSharedReference.getVehiclerNM(getBaseContext()).length()>0)
{
最后一个字符串vname=saveSharedReference.getVehiclenm(getBaseContext());
//Log.v(“设置vname”,即“+vname”);
TextView setuser=(TextView)findViewById(R.id.vname);
setuser.setText(vname);
}
if(saveSharedReference.getVhColor(getBaseContext()).length()>0)
{
最后一个字符串vclr=saveSharedReference.getVhColor(getBaseContext());
//Log.v(“设置vcolor”,即“+vclr”);
TextView setvclr=(TextView)findViewById(R.id.vcolor);
setvclr.setText(vclr);
}
if(saveSharedReference.getNumPlate(getBaseContext()).length()>0)
{
最后一个字符串vplate=saveSharedReference.getNumPlate(getBaseContext());
//Log.v(“设置vplate”、“设置vplate+vplate”);
TextView setvplt=(TextView)findViewById(R.id.nplate);
setvplt.setText(vplate);
}
}
捕获(例外e){
Log.e(“我的错误”,e.toString());
}
按钮lgt=(按钮)findViewById(R.id.btn_lgt);
lgt.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
新线程(newrunnable()){
@凌驾
公开募捐{
//TODO自动生成的方法存根
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost-HttpPost=newhttppost(ServerConnection.ip+“logout.jsp”);
List nameValuePairs=新的ArrayList(2);
//Log.v(“注销的pswd id:”,“+pswd”);
添加(新的BasicNameValuePair(“密码”和“+pswd”);
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
ResponseHandler ResponseHandler=新BasicResponseHandler();
String response=httpclient.execute(httppost,responseHandler);
reverseString=response.trim();
//Log.v(“来自注销的响应设备id”,反向限制);
}
捕获(例外e){
//Log.e(“注销错误:,e.toString());
runOnUiThread(新的Runnable()
{
@凌驾
公开募捐
{
if(AppStatus.getInstance(getBaseContext()).isOnline(getBaseContext())){
Toast.makeText(getBaseContext(),“注销错误”,Toast.LENGTH_SHORT.show();
}
否则{
Toast.makeText(getBaseContext(),“连接错误”,Toast.LENGTH_SHORT.show();
}
} });
}
如果(反向限制!=null){
saveSharedReference.clearUserDetails(getBaseContext());
Intent myIntent=newintent(getBaseContext(),login.class);
星触觉(myIntent);
完成();
}
}
}).start();
}
});
}
@凌驾
已更改位置上的公共无效(位置){
//TODO自动生成的方法存根
TextView txtLoc=(TextView)findViewById(R.id.textView1);
if(latitude==String.valueOf(location.getLatitude())&&longitude==String.valueOf(location.getLongitude()))
{
返回;
}
latitude=String.valueOf(location.getLatitude());
经度=String.valueOf(location.getLongitude());
final int driver_id=saveSharedReference.getUserId(getBaseContext());
线程线程=新线程(){
@凌驾
public void run(){
//JSONObject j=新的JSONObject();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost-HttpPost=newhttppost(ServerConnection.ip+“updatedata.jsp”);
试一试{
//j、 put(“lat”,纬度);
//j、 put(“lon”,经度);
//Toast.makeText(getBaseContext(),“”+j.toString(),Toast.LENGTH_LONG).show();
List nameValuePairs=新的ArrayList(3);
添加(新的BasicNameValuePair(“lat”,纬度));
添加(新的BasicNameValuePair(“lon”,经度));
添加(新的BasicNameValuePair(“密码”和“+pswd”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
ResponseHandler ResponseHandler=新BasicResponseHandler();
String response=httpclient.execute(httppost,responseHandler);
//这是来自jsp应用程序的响应
字符串反向限制=
locationManager.removeUpdates(MainActivity.this)