目前所做项目,后台与前端是利用header中的cookie进行判断登录状态
若本次发送给后台的cookie是未登录状态,则跳转至登录页面要求用户登录
public void onResponse(String s)
{
try {
JSONObject obj = new JSONObject(s);
if (obj.has("statusCode"))
{
Intent unlogin = new Intent();
unlogin.setAction("UNLOGIN");
MyApplication.getInstance().getApplicationContext().sendBroadcast(unlogin);
return;
}
//其他正常借口逻辑
}catch(JSONException e){...}
}
发送Action为“UNLOGIN”的广播
在BaseActivity中 声明广播,并做自己的逻辑
BroadcastReceiver mybroad=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (!ParamsUtil.getInstance().isLogin){
ParamsUtil.getInstance().setLogin(true);
Intent intent1 = new Intent(context, LoginAct.class);
startActivity(intent1);
}
}
};
oncreate中注册
IntentFilter filter = new IntentFilter();
filter.addAction("UNLOGIN");
this.registerReceiver(mybroad, filter);
别忘记在onDestory中解绑!
@Override
protected void onDestroy() {
super.onDestroy();
this.unregisterReceiver(mybroad);
}