正式和測試環境之間切換
阿新 • • 發佈:2018-12-15
工具類如下:
package com.websocket.demo; import android.content.Context; import java.io.File; import java.io.IOException; import java.lang.ref.SoftReference; /** * Author:chenp * Time : 2018/10/16 * Description :正式和測試環境切換 */ public class NetEnvironmenApi { private static final int COUNT = 8;//連續點選一個按鈕次數,切換為測試環境 private static final int TIP_COUNT = 4; private long lastClickTime = 0;//上一次點選的時間 private int clickCount = 0;//點選的次數 private SoftReference<Context> context; private static final NetEnvironmenApi ourInstance = new NetEnvironmenApi(); public static NetEnvironmenApi getInstance() { return ourInstance; } private NetEnvironmenApi() { } public void setContext(SoftReference<Context> context) { if (context != null) { this.context = context; } } /** * 增加一次點選 */ public boolean checkAdd() { long time = System.currentTimeMillis(); if (time - lastClickTime > 1000) {//兩次間隔時間過長,重新計算 lastClickTime = time; clickCount = 1; } else { clickCount++; lastClickTime = time; } if (clickCount >= COUNT) { return true; } else { return false; } } public int getLeftCount() { return COUNT - clickCount; } /** * 是否顯示Toast * * @return */ public boolean isShowTips() { return clickCount >= TIP_COUNT; } /** * 建立標記為測試環境的檔案 */ public boolean createDebugFile() { try { if (context == null || context.get() == null) { return false; } File file = new File(context.get().getCacheDir(), FILE_NAME); if (!file.exists()) { return file.createNewFile(); } else { return true; } } catch (IOException e) { } return false; } private String FILE_NAME = "debug"; /** * 是否為測試環境 * * @return */ public boolean isDebugEnv() { if (context == null || context.get() == null) { return false; } File file = new File(context.get().getCacheDir(), FILE_NAME); if (file.exists()) { return true; } else { return false; } } /** * 切換為正式環境 * * @return */ public boolean changeToRelease() { if (context == null || context.get() == null) { return false; } File file = new File(context.get().getCacheDir(), FILE_NAME); if (file.exists()) { return file.delete(); } return true; } }
然後點選多次進入測試環境:
if (NetEnvironmenApi.getInstance().checkAdd()) { boolean isCreate = NetEnvironmenApi.getInstance().createDebugFile(); if (isCreate) { Toast.makeText(MainActivity.this, "切換為測試環境", Toast.LENGTH_LONG).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(getApplication().getPackageName()); LaunchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(LaunchIntent); android.os.Process.killProcess(android.os.Process.myPid()); } }, 500); } else { Toast.makeText(MainActivity.this, "切換為測試環境失敗", Toast.LENGTH_LONG).show(); } } else { if (NetEnvironmenApi.getInstance().isShowTips()) { Toast.makeText(MainActivity.this, "再點選" + NetEnvironmenApi.getInstance().getLeftCount() + "次進入測試環境", Toast.LENGTH_LONG).show(); } }
點選按鈕,切換為正式環境:
if (NetEnvironmenApi.getInstance().changeToRelease()) { Toast.makeText(MainActivity.this, "切換為正式環境", Toast.LENGTH_LONG).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage(getApplication().getPackageName()); LaunchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(LaunchIntent); android.os.Process.killProcess(android.os.Process.myPid()); } }, 500); }
其實就是在手機的SD卡中增加一個檔案來判斷。