Android:当我在 Sql server 中将状态更改为 false 时,如何从我的应用程序中自动注销?
Posted
技术标签:
【中文标题】Android:当我在 Sql server 中将状态更改为 false 时,如何从我的应用程序中自动注销?【英文标题】:Android : How can i Auto Log out from my app when i change my status to false in Sql server? 【发布时间】:2016-05-31 20:47:53 【问题描述】:我在我的应用程序中使用了以下内容: 1] MainActivity [登录页面] 2] 欢迎[主页]
我使用了 .NET Web 服务并且 数据库:表名 tblLogin 字段是: id , 姓名 , 密码 , 状态
1] MainActivity.java
package com.example.yashdesai.myapplication;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity
final String NAMESPACE = "http://tempuri.org/";
final String METHOD_NAME = "Login";
final String SOAP_ACTION = "http://tempuri.org/Login";
final String URL = "http://192.168.11.16/Lucky/WebService.asmx";
EditText tv;
EditText tv1;
Button btn;
TextView tv2;
SessionManager session;
@Override
public void onBackPressed()
finish();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
session = new SessionManager(getApplicationContext());
tv = (EditText) findViewById(R.id.name);
tv1 = (EditText) findViewById(R.id.password);
tv2 = (TextView) findViewById(R.id.text);
btn = (Button) findViewById(R.id.button);
// sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();
if(session.isLoggedIn()==true)
HashMap<String, String> user = session.getUserDetails();
String name = user.get(SessionManager.KEY_NAME);
String password = user.get(SessionManager.KEY_PASSWORD);
Intent i = new Intent(getApplicationContext(),welcome2.class);
i.putExtra("name",name);
startActivity(i);
// session.checkLogin();
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View arg0)
new MyTask().execute();
);
public class MyTask extends AsyncTask<Void, Void, String>
String response = "";
public void onPreExecute()
super.onPreExecute();
@Override
protected String doInBackground(Void... arg0)
final SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("name", tv.getText().toString());
request.addProperty("password", tv1.getText().toString());
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
response = result.toString();
Log.d("REsponse rsult: ", response.toString());
catch (SoapFault soapFault)
soapFault.printStackTrace();
catch (IOException e)
response = "1";
catch (XmlPullParserException e)
response = "2";
return response;
@Override
public void onPostExecute(String res)
if(!(res.equalsIgnoreCase("")))
tv2.setText(res);
if(tv2.getText().toString().equalsIgnoreCase("not valid"))
Toast.makeText(getApplicationContext(), "Incorrect username or password!", 9000).show();
else
session.createLoginSession(tv.getText().toString(), tv1.getText().toString());
Intent i=new Intent(getApplicationContext(),welcome2.class);
i.putExtra("name",tv.getText().toString());
startActivity(i);
2] Welcome2.java
public class welcome2 extends AppCompatActivity
TextView t;
String user_status="off";
SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome2);
session = new SessionManager(getApplicationContext());
t=(TextView)findViewById(R.id.wname);
Intent intent = getIntent();
String value = intent.getStringExtra("name");
t.setText("Welcome: " + value);
public void logout(View view)
session.logoutUser();
【问题讨论】:
【参考方案1】: timer = new CountDownTimer(1 *10 * 1000, 1000)
public void onTick(long millisUntilFinished)
//Some code
// timer.start();
t.setText(Long.toString(millisUntilFinished/1000));
public void onFinish()
if(tv.getText().toString().equalsIgnoreCase("not valid"))
if(tv1.getText().toString().equalsIgnoreCase("not valid"))
session.logoutUser();
else
.start();
希望它对你有用!
【讨论】:
【参考方案2】: private final Runnable m_Runnable = new Runnable()
public void run()
Toast.makeText(welcome2.this,"in runnable", Toast.LENGTH_SHORT).show();
// Toast.makeText(welcome2.this,"in runnable", Toast.LENGTH_SHORT).show();
//session.logoutUser();
//welcome2.this.mHandler.postDelayed(m_Runnable, 5000);
;
【讨论】:
以上是关于Android:当我在 Sql server 中将状态更改为 false 时,如何从我的应用程序中自动注销?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 SQL Server 2017 中将“生成脚本”与图形数据库对象一起使用?