【转】一个简单的内存同步到数据库的方法

转载自:一个简单的内存同步到数据库的方法


为了提高大量数据运算的效率,我一般都会采用以下方式:
1.数据映射到内存
2.在内存中进行数据运算
3.将结果放入队列
4.队列与数据库同步数据

假设有一张表,并对此表做增删改操作

字段名 类型
Key INT
Value INT

如果操作的次数很多,且每次都直接用数据库更新,这很显然是不现实的
我需要映射到内存中做操作,然后用计时器批量更新数据。
为了保证内存数据与数据库的同步,我构建了以下对象:

Class Unit
{
public int key;
public int value;
public int control;
}

字段control是实现同步的关键。他表示内存中数据的几种状态,分别是:正常(0)、待新增(1)、待更新(2)、待删除(3)、直接删除(4)
状态的解释:
0:数据库映射到内存中时,control = 0,计时器更新时,不理会此条数据
1:New Unit时,control = 1,计时器更新时会把它添加入库
2:对Unit做更新操作时,control = 2,计时器更新时会做数据库更新操作
3:对Unit做删除操作时,control = 3,表示打上删除标记,计时器更新时会做数据库删除操作
4:对New Unit做删除后,control = 4,计时器更新时会直接把对象删除 继续阅读【转】一个简单的内存同步到数据库的方法

Git如何修改已经push到远程仓库的历史commit信息?

节选自:Git如何修改已经push到远程仓库的历史commit信息?


  1. 在修改commit message之前最好先查看一下历史的commit信息,这是一个好习惯。
    git log
  2. 如果你想修改最近的某个commit message,那你可以直接用这个。HEAD~3表示将修改当前版本的近三条commit。
    git rebase -i HEAD~3
  3. 然后我们就可以进行修改了,比如我要改第一条,那我就把“pick”修改为“edit”。
    edit 1bf6b28 init
    pick 01d49ca 优化开发环境数据库目录
    pick 9709bde iview 3.0 & use shortId
    pick 03b3355 collection
  4. 接下来我们使用下面 这个命令,加不加-s都是可以的,-s表示要加入签名。
    git commit --amend
    git commit --amend -s
  5. 然后我就该执行下面这条命令了。
    git rebase --continue
  6. 最后,我们就该push到远程仓库了,完成修改。
    git push origin master

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;
}