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

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

概述我想确保当用户登录时,无论发生什么事情(崩溃,关机/关机/重启,离开应用程序),用户信息数据都将在所有活动中发送应用程序到Web服务器.例如,在应用程序启动时,用户登录“9999”它将转到具有5个差异的主活动.活动.用户9999将发送一个活动(即gps位置),它将以用户9999gps123.234123

我想确保当用户登录时,无论发生什么事情(崩溃,关机/关机/重启,离开应用程序),用户信息数据都将在所有活动中发送应用程序到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变量中的“静态”辅助类中.

总结

以上是内存溢出为你收集整理的Android:用户登录并保持会话直到注销(需要批准)全部内容,希望文章能够帮你解决Android:用户登录并保持会话直到注销(需要批准)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1097089.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存