2016-05-04 70 views
-7

我已经创建了一个应用程序。成功登录后,我重定向到另一个活动,即我的主要活动,但是当我关闭应用程序,我不得不再次登录..我想保持登录,除非我点击注销按钮..如何可以做到这一点? 我已经使用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)); 
      } 
     }); 


    } 
} 
+0

您可以使用共享preferences.see该做http://androidexample.com/Android_Session_Management_Using_SharedPreferences_-_Android_Example /index.php?view=article_discription&aid=127&aaid=147 – Rooban

+0

[如何保持android应用程序始终在sta中登录te?](http://stackoverflow.com/questions/12744337/how-to-keep-android-applications-always-be-logged-in-state) – KishuDroid

+0

@ ksax95你在吗? – sushildlh

回答

0

您可以使用SharedPreferences。 采用布尔值并在登录时将其设置为true。 当你点击注销后,将它设置为False。相应地在启动屏幕后重定向页面。

您可以创建一个sperate类的sharedPreferences。 如:

public class CustomSharedPreferences { 
    public CustomSharedPreferences(Context context) { 
    // TODO Auto-generated constructor stub 
    prefs = getMyPreferences(context); 
    this._context = context; 
    prefs = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); 
    editor = prefs.edit(); 
} 


private static SharedPreferences getMyPreferences(Context context) { 
    return context.getSharedPreferences(Util.APP_PREFERENCES, 
      Context.MODE_PRIVATE); 
} 


public boolean isLogin() { 
    return prefs.getBoolean("isLogin", false); 
} 

public void setIsLogin(boolean isLogin) { 
    this.isLogin = isLogin; 
    editor.putBoolean("isLogin", isLogin).commit(); 
}   
} 

// SplashScreenActivity

public class SplashScreenActivity extends AppCompatActivity { 

private CustomSharedPreferences customSharedPreferences; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_splash_screen); 


    customSharedPreferences = new CustomSharedPreferences(getApplicationContext()); 

    new Handler().postDelayed(new Runnable() { 
     // Using handler with postDelayed called runnable run method 
     @Override 
     public void run() { 
      Intent i; 
      if (customSharedPreferences.isLogin()) { 
       i = new Intent(SplashScreenActivity.this, MainActivity.class); 
      } else { 
       i = new Intent(SplashScreenActivity.this, LoginActivity.class); 
      } 
      startActivity(i); 
      finish(); 
     } 
    }, 5 * 1000); // wait for 5 seconds 
}  
} 

// LoginActivity

public class LoginActivity{ 

    //Object of CustomSharedPreferences Class 
    customSharedPreferences = new CustomSharedPreferences(getApplicationContext()); 

//After Clicking the Login Button 
    customSharedPreferences.setIsLogin(true); 
    } 
+0

可以请你展示如何在我的代码中使用它..我是新的android ..请你的帮助将不胜感激:) :) – ksax95

+0

检查编辑答案@ ksax95 – AmeyaG

+0

您的CustomSharedPreferences文件显示错误 prefs,编辑器,prefname全部无法解析 – ksax95

相关问题