我正在创建登录应用程序,在android studio中使用php, 我的问题是如何使应用程序保持在线状态。
即
当我使用用户名和密码登录应用程序时,应用程序会在我关闭应用程序时登录。它被注销,但我需要在关闭应用程序时保持应用程序登录。还
任何人都可以建议如何做到这一点吗?
这是我的第一个主要活动(登录活动)
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends Activity {
EditText name, password;
String Name, Password;
Context ctx=this;
String NAME=null, PASSWORD=null, EMAIL=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.main_name);
password = (EditText) findViewById(R.id.main_password);
}
public void main_login(View v){
Name = name.getText().toString();
Password = password.getText().toString();
BackGround b = new BackGround();
b.execute(Name, Password);
}
class BackGround extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
String name = params[0];
String password = params[1];
String data="";
int tmp;
try {
URL url = new URL("http://localhost/sample/Login%20php/login.php");
String urlParams = "name="+name+"&password="+password;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1){
data+= (char)tmp;
}
is.close();
httpURLConnection.disconnect();
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "Exception: "+e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Matches")) {
// Intent intent=new Intent(Intent.ACTION_VIEW);
//intent.setData(Uri.parse("content://com.android.calendar/time/"));
//startActivity(intent);
Boolean Registered; final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
Registered = sharedPref.getBoolean("Registered", false);
if (!Registered)
{
startActivity(new Intent(MainActivity.this,MainActivity.class));
}else
{ startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
}
if(result.equals("No match")) {
Toast.makeText(MainActivity.this, "USERNAME/PASSWORD IS INCORRECT", Toast.LENGTH_LONG).show();
}
}
}
}
第二项活动
package com.example.myapplication;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit(); editor.putBoolean("Registered", true);
editor.apply();
}
}
答案 0 :(得分:0)
在MainActivity的onPostExecute中
@Override
protected void onPostExecute(String result) {
if(result.equals("Matches")) {
startActivity(new Intent(MainActivity.this,Main2Activity.class));
finish();
}
if(result.equals("No match")) {
Toast.makeText(MainActivity.this, "USERNAME/PASSWORD IS INCORRECT", Toast.LENGTH_LONG).show();
}
}
}
创建启动画面并在清单
中设置启动器活动public class Splash extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Boolean Registered;
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(Splash.this);
Registered = sharedPref.getBoolean("Registered", false);
if (!Registered)
{
startActivity(new Intent(Splash.this,MainActivity.class));
}else {
startActivity(new Intent(Splash.this,SecondAcivity.class));
}
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
您的Main2Activity活动是正确的