正式和测试环境之间切换

工具类如下:

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卡中增加一个文件来判断。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值