Android:用户登录并保持会话直到注销(需要批准) [英] Android: user login and stays in session until logout (which needs approval)

查看:29
本文介绍了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:用户登录并保持会话直到注销(需要批准)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆