Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class Main {
    private static final String CONFIG_NAME = "update_config";
    private static final String APP_ACTIVE_TIME = "app_active_time";
    private static final String APP_LAST_VERSION = "app_last_version";
    private static final String APP_IS_UPDATE = "app_is_update";

    public static void updateConfig(Context context) {
        SharedPreferences sp = getSP(context);
        SharedPreferences.Editor editor = sp.edit();
        PackageManager pm = context.getPackageManager();

        if (!sp.contains(APP_ACTIVE_TIME)) {
            editor.putLong(APP_ACTIVE_TIME, System.currentTimeMillis());
            editor.putBoolean(APP_IS_UPDATE, false);
        } else {
            editor.putBoolean(APP_IS_UPDATE, true);
        }

        try {
            PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
            String versionName = info.versionName;
            editor.putString(APP_LAST_VERSION, versionName);
        } catch (PackageManager.NameNotFoundException e) {
        }

        editor.apply();
    }

    private static SharedPreferences getSP(Context context) {
        return context.getSharedPreferences(CONFIG_NAME, Context.MODE_PRIVATE);
    }
}