Android判断应用在前台和切换应用到前台的方法

/**
 * 判断应用在前台
 */
public static boolean isForeground(Context context) {
    boolean isForeground = false;
    ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    if (activityManager != null) {
        ComponentName componentName = activityManager.getRunningTasks(1).get(0).topActivity;
        if (componentName != null) {
            isForeground = componentName.getPackageName().equals(context.getPackageName());
        }
    }
    if (isForeground) {
        isForeground = isForegroundByProcess();
    }
    return isForeground;
}

/**
 * 根据进程判断应用在前台
 */
private static boolean isForegroundByProcess() {
    boolean isForeground = true;
    String filePath = "/proc/" + android.os.Process.myPid() + "/cgroup";
    try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
        String line = br.readLine();
        if (line != null && line.contains("bg_non_interactive")) {
            isForeground = false;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return isForeground;
}

/**
 * 切换应用到前台
 */
public static void switchToForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks(100);
    for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
        ComponentName componentName = taskInfo.topActivity;
        if (componentName != null && componentName.getPackageName().equals(context.getPackageName())) {
            activityManager.moveTaskToFront(taskInfo.id, 0);
            break;
        }
    }
}

参考资料:

判断App位于前台或者后台的6种方法

Android 将后台应用切换到前台

微信数据库解密

节选自:微信数据库解密


  • 微信数据库加密方式:
1.获取手机IMEI码

2.获取当前登录微信账号的uin(存储在sp里面)

3.拼接IMEI和uin

4.将拼接完的字符串进行md5加密

5.截取加完密的字符串的前七位(字母必须为小写)

上面可以看到就两个变量,`uin`和`imei`

代码送上直接使用:https://github.com/l123456789jy/WxDatabaseDecryptKey

Android点击EditText文本框之外任意位置隐藏键盘

实现思路:通过 dispatchTouchEvent 每次 ACTION_DOWN 事件中动态判断非 EditText 本身区域的点击事件,然后在事件中进行屏蔽。

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        getWindow().getDecorView().postDelayed(() -> {
            View view = getCurrentFocus();
            if (isShouldHideInput(view, ev)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    view.clearFocus();
                }
            }
        }, 100);
    }
    return super.dispatchTouchEvent(ev);
}

private boolean isShouldHideInput(View v, MotionEvent event) {
    if (v instanceof EditText) {
        int[] leftTop = new int[]{0, 0};
        //获取输入框当前的location位置
        v.getLocationInWindow(leftTop);
        int left = leftTop[0];
        int top = leftTop[1];
        int bottom = top + v.getHeight();
        int right = left + v.getWidth();
        return !(event.getRawX() > left && event.getRawX() < right && event.getRawY() > top && event.getRawY() < bottom);
    }
    return false;
}

参考链接:

Android点击EditText文本框之外任何地方隐藏键盘的解决办法

Android取消EditText自动默认获取焦点行为

转载自:Android取消EditText自动默认获取焦点行为


Android控件EditText自动默认会获取屏幕焦点,取消这个行为只需要两行代码,如下:

android:focusable="true"
android:focusableInTouchMode="true"

我们只需要把这两行代码添加至EditText控件的父级控件中,把屏幕焦点设置到父级控件上,EditText就会暂时失去焦点。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <EditText
        android:id="@+id/et_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

Android 判断网络连接是否可用

/**
 * 判断网络是否已连接
 */
public static boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    if (info != null) {
        return info.isConnected();
    }
    return false;
}
/**
 * 使用ping判断网络连接
 */
public static boolean ping() {
    try {
        Process process = Runtime.getRuntime().exec("ping -c 1 www.baidu.com");
        int exitCode = process.waitFor();
        return (exitCode == 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

ProductFlavors 简单使用

节选自:ProductFlavors 简单使用


概述

我们在开发过程中,会经常遇到,同样的业务逻辑,需要配置不同的资源的情况。有时是不同的渠道,有时是不同的语言环境,各种不同。

如果我们在业务逻辑中,去实现这些差异化,会导致两个问题:

  1. 代码量很大,分支路径很多。而很多路径在实际使用中是用不到的,造成代码质量差。
  2. 用业务逻辑去区分情况加载资源,会导致包内需要包含大量的资源,造成包体积过大。

所以,我们需要在编译阶段,就区分种种情况,给予相应的资源。

Android中build.gradleproductFlavors为我们提供了这样的能力。

快速使用

在module的build.gradle中,我们通过flavorDimensionsproductFlavors的配合,来实现多维度的区分:

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.oceanlong.buildtestpro"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    ...

    flavorDimensions "country" 

    productFlavors {
        china {
            applicationId "com.example.oceanlong.buildtestfree"
            dimension "country"
        }
        usa {
            applicationId "com.example.oceanlong.buildtestpro"
            dimension "country"
        }
    }
    ...
}

Tmux(终端复用器)

tmux是什么

tmux是一个 terminal multiplexer(终端复用器),它可以启动一系列终端会话。

我们使用命令行时,打开一个终端窗口,会话开始,执行某些命令如npm run dev,关闭此终端窗口,会话结束,npm run dev服务会话随之被关闭。有时我们希望我们运行的服务如npm run dev 或者一些cd命令等,被保留,而不是关闭窗口再打开后,重新手动执行。tmux的主要用途就在于此。

它解绑了会话和终端窗口。关闭终端窗口再打开,会话并不终止,而是继续运行在执行。将会话与终端窗口彻底分离。 继续阅读Tmux(终端复用器)