我已经创建了一个应用程序。成功登录后,我重定向到另一个活动,即我的主要活动,但是当我关闭应用程序,我不得不再次登录..我想保持登录,除非我点击注销按钮..如何可以做到这一点? 我已经使用php数据库来保存登录信息和后台工作人员重定向到另一个活动。 请help..i必须提交我的项目在3天的时间...在此先感谢:d如何在我的android应用程序中实现登录和注销功能?
登录页面: -
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.opengl.ETC1;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class login extends Activity {
SharedPreferences prefs;//new line
EditText UsernameEt, PasswordEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
prefs = PreferenceManager.getDefaultSharedPreferences();//new line
UsernameEt = (EditText)findViewById(R.id.etUserNamez);
PasswordEt = (EditText)findViewById(R.id.etPasswordz);
if(prefs.getBoolean("locked", false)){//new line
Intent intent = new Intent(context,Main.class);//new line
startActivity(intent);//new line
}//new line
}
public void OnLogin(View view) {
String username = UsernameEt.getText().toString();
String password = PasswordEt.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username, password);
}
}
BackgroundWorker的活动: -
package com.project.v_app;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx){
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.0.103/login.php";
if(type.equals("login"))
{
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!=null)
{
result +=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
if(result!=null && result.equals("Login Not Success")){
alertDialog.setMessage(result);
alertDialog.show();
}
if(result!=null && result.equals("Login Success")){
prefs.edit().putBoolean("locked", true).commit();//new line
Intent intent = new Intent(context,Main.class);
context.startActivity(intent);
}
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}
主要活动: -
package com.project.v_app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button tt=(Button) findViewById(R.id.loginbutton);
tt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, timetable.class));
}
});
Button uc= (Button) findViewById(R.id.button2);
uc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, upcoming.class));
}
});
Button rem= (Button) findViewById(R.id.button3);
rem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, reminder.class));
}
});
Button ev= (Button) findViewById(R.id.button4);
ev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, events.class));
}
});
Button news= (Button) findViewById(R.id.button5);
news.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, news.class));
}
});
Button fb= (Button) findViewById(R.id.button6);
fb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Main.this, feedback.class));
}
});
}
}
您可以使用共享preferences.see该做http://androidexample.com/Android_Session_Management_Using_SharedPreferences_-_Android_Example /index.php?view=article_discription&aid=127&aaid=147 – Rooban
[如何保持android应用程序始终在sta中登录te?](http://stackoverflow.com/questions/12744337/how-to-keep-android-applications-always-be-logged-in-state) – KishuDroid
@ ksax95你在吗? – sushildlh