工具类如下:
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卡中增加一个文件来判断。