android判斷使用者是否已登陸詳細程式碼
阿新 • • 發佈:2019-02-14
package com.sanyue.jianzhicompany;
import java.lang.ref.WeakReference;
import org.android.agoo.client.BaseRegistrar;
import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os .Handler;
import android.os.Message;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.sanyue.jianzhicompany.convert.UserJSONConvert;
import com.sanyue.jianzhicompany.db.Preferences;
import com.sanyue.jianzhicompany.http.AsyncHttpResponseHandler;
import com.sanyue.jianzhicompany.http .RequestParams;
import com.sanyue.jianzhicompany.http.RestClient;
import com.sanyue.jianzhicompany.model.User;
import com.sanyue.jianzhicompany.utils.Constant;
import com.sanyue.jianzhicompany.utils.ToastUtil;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;
/*
*
* 判斷是否登陸
* 是:更新登陸資訊
* 否:轉到登陸介面登陸
*
* */
public class SplashActivity extends Activity {
private static final long DELAY_MILLIS = 1 * 1000;
private static final int MSG_WHAT_STARTMAIN = 1;
public static String device_token;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PushAgent mPushAgent = PushAgent.getInstance(SplashActivity.this);
device_token = BaseRegistrar.getRegistrationId(SplashActivity.this); //通過“友盟”獲取裝置資訊
PushAgent.getInstance(SplashActivity.this).onAppStart();
mPushAgent.enable();
Preferences.setDeviceToken(device_token);
setContentView(R.layout.activity_splash);
mHandler = new MyHandler(this);
mHandler.sendEmptyMessageDelayed(MSG_WHAT_STARTMAIN, DELAY_MILLIS);
if (Preferences.isLogin()) {
// 更新登陸資訊
getMyInfo();
}
}
private static class MyHandler extends Handler {
WeakReference<SplashActivity> mActivity;
MyHandler(SplashActivity activity) {
mActivity = new WeakReference<SplashActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
SplashActivity theActivity = mActivity.get();
if (theActivity != null) {
switch (msg.what) {
case MSG_WHAT_STARTMAIN:
if (Preferences.isLogin()) {
// 更新版本資訊
Preferences.updateVersionCode();
theActivity.startActivity(new Intent(theActivity,
MainActivity.class));
} else {
theActivity.startActivity(new Intent(theActivity,
LoginActivity.class));
Preferences.updateVersionCode();
}
theActivity.finish();
break;
}
}
}
}
private void getMyInfo() {
RequestParams params = new RequestParams();
int id = Preferences.getAccountId();
String token = Preferences.getToken();
params.put("user_id", id);
params.put("version", 1.0);
params.put("app_name","android_jianzhi" );
params.put("platform", "android_jianzhi");
params.put("token", token);
RestClient.post(Constant.API_GET_COMPANY_INFO, params, new AsyncHttpResponseHandler(getApplicationContext(), new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode, Header[] headers,
JSONObject response) {
// TODO Auto-generated method stub
super.onSuccess(statusCode, headers, response);
try {
if (response.getInt("msg_code") == Constant.CODE_SUCCESS) {
User user = UserJSONConvert.convertJsonToItem(response.getJSONObject("data"));
ToastUtil.make(SplashActivity.this).show(""+user);
Preferences.updateAccountUser(user);
} else {
ToastUtil.make(SplashActivity.this).show(response.getInt("msg"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}));
}
}
Preference類:
import org.json.JSONObject;
import com.sanyue.jianzhicompany.model.User;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
public final class Preferences {
public static SharedPreferences sSHARED_REFERENCES = null;
private static Context sAPPLICATION_CONTEXT;
private static String sDEVICE_ID;
private static String sVERSION_NAME;
private static int sUSER_ID = -1;// 說明沒有初始化
public Preferences() {
}
public static boolean isLogin() {
return getAccountId() > 0 ? true : false;
}
public static boolean isShowWelcome() {
try {
PackageInfo info = sAPPLICATION_CONTEXT.getPackageManager()
.getPackageInfo(sAPPLICATION_CONTEXT.getPackageName(), 0);
// 當前版本的版本號
int versionCode = info.versionCode;
int saveVersionCode = sSHARED_REFERENCES.getInt("version_code", 0);
return versionCode != saveVersionCode;
} catch (NameNotFoundException e) {
return true;
}
}
public static boolean updateVersionCode() {
try {
PackageInfo info = sAPPLICATION_CONTEXT.getPackageManager()
.getPackageInfo(sAPPLICATION_CONTEXT.getPackageName(), 0);
// 當前版本的版本號
int versionCode = info.versionCode;
Editor editor = sSHARED_REFERENCES.edit();
editor.putInt("version_code", versionCode);
return editor.commit();
} catch (NameNotFoundException e) {
return false;
}
}
public static void init(Context context) {
if (sSHARED_REFERENCES == null) {
sAPPLICATION_CONTEXT = context.getApplicationContext();
sSHARED_REFERENCES = PreferenceManager
.getDefaultSharedPreferences(sAPPLICATION_CONTEXT);
}
}
public static SharedPreferences getSharedPreferences() {
return sSHARED_REFERENCES;
}
public static String getDeviceId() {
if (sDEVICE_ID == null) {
sDEVICE_ID = ((TelephonyManager) sAPPLICATION_CONTEXT.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
}
return sDEVICE_ID;
}
public static String getVersionName() {
if (sVERSION_NAME == null) {
try {
PackageInfo info = sAPPLICATION_CONTEXT.getPackageManager()
.getPackageInfo(sAPPLICATION_CONTEXT.getPackageName(),
0);
// 當前應用的版本名稱
sVERSION_NAME = info.versionName;
// 當前版本的版本號
// int versionCode = info.versionCode;
// 當前版本的包名
// String packageNames = info.packageName;
} catch (NameNotFoundException e) {
}
}
return sVERSION_NAME;
}
/**
* 獲取當前登入的使用者
*
* @return 如果已經登入返回User,否則返回null
*/
public static User getAccountUser() {
User user = new User();
sUSER_ID = sSHARED_REFERENCES.getInt("user_id", 0);
if (sUSER_ID == 0) {
return null;
}
JSONObject jsonObject;
// jsonObject = new JSONObject(sSHARED_REFERENCES.getString(
// "user_json", null));
user.setToken(sSHARED_REFERENCES.getString("access_token", null));
user.setUserId(sSHARED_REFERENCES.getInt("user_id", 0));
user.setBindName(sSHARED_REFERENCES.getString("bind_name", null));
return user;
}
public static boolean setAccountUser(User user, String accessToken) {
Editor editor = sSHARED_REFERENCES.edit();
editor.putString("access_token", accessToken);
editor.putInt("user_id", user.getUserId());
// editor.putString("user_json", user.toJSON().toString());
// TODO json format save
if (editor.commit()) {
sUSER_ID = user.getUserId();
return true;
}
return false;
}
public static boolean setDeviceToken(String device_token){
Editor editor = sSHARED_REFERENCES.edit();
editor.putString("device_token", device_token);
if(editor.commit()){
return true;
}
return false;
}
public static String getDeviceToken(){
String device_token = sSHARED_REFERENCES.getString("device_token", null);
return device_token;
}
public static boolean userClear() {
Editor editor = sSHARED_REFERENCES.edit();
editor.putString("access_token", "");
editor.putInt("user_id", 0);
editor.putString("create_time", "");
// TODO json format save
if (editor.commit()) {
sUSER_ID = 0;
return true;
}
return false;
}
public static boolean updateAccountUser(User user) {
Editor editor = sSHARED_REFERENCES.edit();
editor.putInt("user_id", user.getUserId());
editor.putString("user_json", user.toJSON().toString());
if (editor.commit()) {
sUSER_ID = user.getUserId();
return true;
}
return false;
}
/**
* 獲取登入使用者的ID
*
* @return 如果已登入返回使用者ID,否則返回0
*/
public static int getAccountId() {
if (sUSER_ID == -1) {
sUSER_ID = sSHARED_REFERENCES.getInt("user_id", 0);
}
return sUSER_ID;
}
public static String getToken() {
return sSHARED_REFERENCES.getString("access_token", null);
}
}