我有将被定向到登录活动的启动活动,如果登录为真,那么用户将直接回到主页,但是当用户按下主页时,应用程序将自动关闭。但问题是当用户打开应用程序时,他们必须再次登录,即使他们之前没有注销。那么如何将水溅到家庭活动中呢?我已阅读,要处理它,我必须使用 sharedpreferences 所以我必须使用它,但我不知道如何使用它自动处理登录。这是我的垃圾:
public class SplashEpolicy extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//hide title bar
BasicDisplaySettings.toggleTaskBar(SplashEpolicy.this, false);
//show status bar
BasicDisplaySettings.toggleStatusBar(SplashEpolicy.this, true);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_epolicy);
Thread timer = new Thread() {
public void run() {
try {
//this is configuration for how long the splash will be shown.
//
int time = Integer.parseInt(getResources().getString(R.string.splashTransTime));
sleep(time);
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
//line below is for direct new activity that will be run after
//current activity finished.
Intent intent = new Intent(SplashEpolicy.this,EpolicyMainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
}
};
timer.start();
}
这是我包含共享首选项的登录活动:
String KEY = jo.getString("key");
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("etUser", strUser);
editor.putString("key",KEY);
editor.commit();
System.out.println(KEY+""+"key"+""+"etUser");
Dialog.dismiss();
Intent i = new Intent(LoginActivity.this,EpolicyMainTab.class);
startActivity(i);
}
}catch (JSONException e) {
e.printStackTrace();
}
所以这是我的家(EpolicyListPolisActivity):
public class EpolicyListPolis extends ListActivity {
static String PEMEGANG="PEMEGANG";
static String POLIS="POLIS";
static String STATUS="STATUS";
static String TERTANGGUNG="TERTANGGUNG";
String KEY, strUser;
List NasabahList= new ArrayList();
private ProgressDialog Dialog;
private String strStoragePath = "",
strNameFileDiv = "";
ImageView LogoutButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.Nomor_Polis);
setContentView(R.layout.epolicy_list_polis);
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
strUser= settings.getString("etUser", null);
KEY = settings.getString("key", null);
LogoutButton=(ImageView)findViewById(R.id.LogoutButton);
new NasabahAsyncTask().execute();
LogoutButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
dialogSignOut();
}
});
}
public class NasabahAsyncTask extends AsyncTask<Void, Void, String>{
String url = ("http://www.example.com?i="+strUser+"&k="+KEY);
public NasabahAsyncTask() {
this.url=url;
// TODO Auto-generated constructor stub
}
protected void onPreExecute() {
super.onPreExecute();
Dialog = ProgressDialog.show(EpolicyListPolis.this, "", "Melakukan Pengambilan Data");
}
@Override
protected String doInBackground(Void... params) {
String result="";
try {
result=Connection.get(url);
}catch (Exception e){
result=" ";
Log.d("test viewer",e.getMessage());
}
return result;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
fetchResponse(result.replace("\n", "").trim());
ListAdapter adapter = new SimpleAdapter(
EpolicyListPolis.this, NasabahList, R.layout.epolicy_polis_listitem,
new String[] {POLIS, PEMEGANG, TERTANGGUNG, STATUS}, new int [] {R.id.polis, R.id.pemegang, R.id.tertanggung, R.id.status});
setListAdapter(adapter);
Dialog.dismiss();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String POLIS = ((TextView)view.findViewById(R.id.polis)).getText().toString();
SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("POLIS", POLIS);
editor.commit();
}
}
private void fetchResponse(String result) {
// TODO Auto-generated method stub
if (!result.equals("")) {
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
if (jsonObject.has("PEMEGANG"))
map.put("PEMEGANG", jsonObject.get("PEMEGANG").toString());
if (jsonObject.has("POLIS"))
map.put("POLIS", jsonObject.get("POLIS").toString());
if (jsonObject.has("STATUS"))
map.put("STATUS", jsonObject.get("STATUS").toString());
if (jsonObject.has("TERTANGGUNG"))
map.put("TERTANGGUNG", jsonObject.get("TERTANGGUNG").toString());
NasabahList.add(map);
System.out.println("json oke");
}
}catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onBackPressed() {
dialogExit();
}
public void dialogExit()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("do you want to exit?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent itSplashEnd = new Intent(EpolicyListPolis.this, SplashEnd.class);
//setIntent.addCategory(Intent.CATEGORY_HOME);
itSplashEnd.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
itSplashEnd.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(itSplashEnd);
finish();
System.exit(0);
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void dialogSignOut()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Sign out?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// what will i do here?
Intent itSignOut = new Intent(EpolicyListPolis.this, EpolicyMainActivity.class);
itSignOut.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(itSignOut);
finish();
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
那么如何在不使用共享首选项再次登录活动的情况下自动登录到主页?以及如何在每次用户退出时清除保存在 sharedpreferences 中的数据?