微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android:用户登录并保持会话直到注销(需要批准)

我想确保当用户登录时,无论发生什么事情(崩溃,关机/关机/重启,离开应用程序),用户信息数据都将在所有活动中发送应用程序到Web服务器.

例如,在应用程序启动时,用户登录“9999”它将转到具有5个差异的主活动.活动.用户9999将发送一个活动(即gps位置),它将以用户9999 gps 123.234 123.123将该信息发送到网络服务器.

我想确保用户保持会话状态,并使用发送的“活动”数据发送其用户数据.
我看了这个链接

What is the most appropriate way to store user settings in Android application

我仍然无法把它放在一起.

同时在同一主屏幕中,它有一个注销.用户需要经理批准通过输入代码(即1234)来完全注销并让新用户输入他们的ID号.我想知道如何将硬编码’1234’放在活动中.

这段代码是我登录后的主屏幕给你的想法

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

更新:

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

解决方法:

您包含的链接显示了存储用户ID的方式 – 您可以使用SharedPreferences,也可以将其存储为in the database.

您可以在任何地方存储“批准代码”.如果要对其进行硬编码,可能需要将其放在public static final String变量中的“静态”辅助类中.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐