2011-05-18 42 views
0

我想确保当用户登录时,无论发生什么事情(崩溃,关机/关机/重启,离开应用程序)同时将用户信息数据与应用程序中的所有活动一起发送到Web服务器。Android:用户登录,并保持会话,直到注销(需要批准)

例如,在应用程序启动时,用户登录'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"); 
     } 
    } } 

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

回答

4

包括你的链接显示存储的方式用户的ID - 您可以使用SharedPreferences或者您可以将其存储in the database

您可以在任何地方存储“审批代码”。如果你想对它进行硬编码,你可能需要将它放在一个“静态”辅助类中,在public static final String变量中。

+0

您是否拥有批准代码和硬代码的注销部分示例代码?即使手机关闭或关闭后台运行应用程序,SharedPreferences是否仍将用户保留在应用程序中?我无法找到sharedPreferences的良好示例代码。 – merrill 2011-05-18 16:42:15

+0

在你包含的SO链接中,fiXedd有一个带有SharedPrefs示例代码的帖子。它将在重新启动后保持该值。对于注销位,没有什么特别的 - 只需读取“manager”放入文本框的值并将其与“UtilClass.HARD_CODED_LOGOT_CODE”进行比较。 – Haphazard 2011-05-18 16:48:59

+0

我仍然很难尝试将它融合在一起。我用Login.java更新了我的帖子,不确定将SharedPrefs放在哪里。 – merrill 2011-05-18 20:43:08

相关问题