伙计们,我正在安卓2.2上工作。我有一种情况,当用户进入应用程序时,他将被允许使用提供的默认音频文件。
现在有一个按钮,当他点击它就会进入登录页面。如果他不是注册用户,他可以注册自己,然后使用用户名和密码再次登录。一旦他使用用户名和密码登录,他将被允许从服务器下载n个音频文件。
因此,我想做的是,一旦用户是注册用户并登录并退出应用程序,但如果注册用户想下载更多文件,则不应提示他用户名和密码,因为他是注册用户。
如何做到这一点?
注册用户登录代码:
ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("userid", userid.getText().toString()));
nvp.add(new BasicNameValuePair("password", password.getText().toString()));
String un = userid.getText().toString();
String pass = password.getText().toString();
System.out.println("user name is " + un);
System.out.println("password is " +pass);
// Log.e(""+sid.getText().toString(),"0");
// Log.e(""+sname.getText().toString(),"0");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsite.com/login.php");
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
try {
BufferedReader bf = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(bf.readLine()+ "'n");
String line="0";
while ((line = bf.readLine()) != null) {
sb.append(line + "'n");
}
is.close();
result=sb.toString();
System.out.println("value of result " +result);
if(result.contains(auth)){
System.out.println("username and password is correct");
Intent authorised = new Intent(Login.this,BrowseFiles.class);
startActivity(authorised);
}else{
System.out.println("incorrect wrong wrong wrong");
error.setText("Incorrect username and password");
}
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
您可以使用应用程序的SharedPreference,并在开始下载时检查它。如果设置了它,则您可以继续打开登录/注册页面,无论您想要
编辑:
一旦用户成功登录
SharedPreferences share_pref=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = share_pref.edit();
editor.putBoolean("Login", true);
// Commit the edits!
editor.commit();
然后在下载之前,您可以检查这个布尔值,如果它是真的,然后进行下载,否则转到登录屏幕