我想確保當用戶登錄時,無論發生什麼事情(崩潰,關機/關機/重啓,離開應用程序)同時將用戶信息數據與應用程序中的所有活動一起發送到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();
}
});
}
}
您是否擁有批准代碼和硬代碼的註銷部分示例代碼?即使手機關閉或關閉後臺運行應用程序,SharedPreferences是否仍將用戶保留在應用程序中?我無法找到sharedPreferences的良好示例代碼。 – merrill 2011-05-18 16:42:15
在你包含的SO鏈接中,fiXedd有一個帶有SharedPrefs示例代碼的帖子。它將在重新啓動後保持該值。對於註銷位,沒有什麼特別的 - 只需讀取「manager」放入文本框的值並將其與「UtilClass.HARD_CODED_LOGOT_CODE」進行比較。 – Haphazard 2011-05-18 16:48:59
我仍然很難嘗試將它融合在一起。我用Login.java更新了我的帖子,不確定將SharedPrefs放在哪裏。 – merrill 2011-05-18 20:43:08