Android: user login and stays in session until logout (which needs approval)

主宰稳场 提交于 2019-12-19 04:56:02

问题


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();
                }
            });
    }
}

回答1:


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.



来源:https://stackoverflow.com/questions/6047691/android-user-login-and-stays-in-session-until-logout-which-needs-approval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!