Android: user login and stays in session until logout (which needs approval)(Android:用户登录并保持会话直到注销(需要批准))
问题描述
I would like to make sure that when user log in it will stay in session no matter what happens (crashed, shut down/power down/reboot, leaving the app) at same time the user info data will be sending with all the activities in the app to the webserver.
for example at the start up of the app, user login '9999' it goes to the main activity that have 5 diff. activities. user 9999 will send one activity (i.e. gps location) it will send that info to the webserver as user 9999 gps 123.234 123.123.
I want to ensure the users stays in session and also send its users data with the "activity" data sent. I read this link
What is the most appropriate way to store user settings in Android application
I was still unable to put it together.
At the same time in the same main screen it has a logout. User needs manager approval to logout by inputting the code(i.e. 1234) to completely logout and for new user to input their id number. I want to know how to put the hardcode '1234' within the activity.
this code is my main screen after login to give you the idea
MainActivity.java
import android.app.ListActivity;
import android.content.Intent; import
android.os.Bundle; import
android.view.View; import
android.widget.ArrayAdapter; import
android.widget.ListView; import
android.widget.TextView;
public class Customer extends ListActivity {TextView selection;
CustomerListItem[] items ={
new CustomerListItem("Start Trip",StartTripActivity.class),
new CustomerListItem("Clock in",ClockinActivity.class),
new CustomerListItem("Customer Svc",CustomerSvcActivity.class),
new CustomerListItem("IndependentInspection",InspectionActivity.class),
new CustomerListItem("Pick Up", PickUpActivity.class),
new CustomerListItem("Log Out", LogoutActivity.class)};
private TextView resultsTxt;
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<CustomerListItem>(
this, android.R.layout.simple_list_item_1,
items));
selection = (TextView) findViewById(R.id.selection);
}
@Override
protected void onListItemClick(ListView l, View v,
int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent intent = new Intent(this,
items[position].getActivity());
startActivityForResult(intent, position);
}
@Override
protected void onActivityResult(int requestCode, int
resultCode, Intent intent)
{
super.onActivityResult(requestCode,
resultCode, intent);
if (resultCode == RESULT_OK)
{
// Perform different actions based on from which activity is
// the application returning:
switch (requestCode)
{
case 0:
// TODO: handle the return of the StartTripActivity
break;
case 1:
// TODO: handle the return of the ClockinActivity
break;
case 2:
// TODO: handle the return of the CustomerSvcActivity
case 3:
// TODO: handle the return of the InspectionActivity
break;
case 4:
// TODO: handle the return of the PickUpActivity
break;
case 5:
// TODO: handle the return of the LogoutActivity
break;
default:
break;
}
}
else if (resultCode == RESULT_CANCELED)
{
resultsTxt.setText("Canceled");
}
} }
UPDATE:
Login.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends Activity {
private EditText etUsername;
private Button btnLogin;
private Button btnCancel;
private TextView lblResult;
/** Called when the activity is first created. */
//@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
etUsername = (EditText)findViewById(R.id.username);
btnLogin = (Button)findViewById(R.id.login_button);
btnCancel = (Button)findViewById(R.id.cancel_button);
lblResult = (TextView)findViewById(R.id.result);
btnLogin.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View v) {
// Check Login
String username = etUsername.getText().toString();
if(username.equals("guest")){
lblResult.setText("Login successful.");
Intent i = new Intent(getApplicationContext(), Customer.class);
startActivity(i);
} else {
lblResult.setText("Login failed. Username doesn't match.");
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
//@Override
public void onClick(View v) {
// Close the application
finish();
}
});
}
}
The link you included shows the way to store the user's ID - you can use SharedPreferences or you can store it in the database.
You can store the "approval code" anywhere. If you want to hard-code it, you may want to put it in a "static" helper class in a public static final String
variable.
这篇关于Android:用户登录并保持会话直到注销(需要批准)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:用户登录并保持会话直到注销(需要批准)
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01