wjc
2025-05-07 5d3efa4c93dde0cde474951e5310bb72ebbf4184
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package com.zxing;
 
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
 
import com.zxing.utils.Strings;
 
public final class ContextHelper {
 
    private static Application application;
    private static Class splashCls;
 
    /**
     * 初始化
     *
     * @param application app
     */
    public static void init(Application application) {
        if (ContextHelper.application == null) {
            ContextHelper.application = application;
        }
    }
 
    public static Context getAppContext() {
        if (application != null) {
            return application.getApplicationContext();
        }
        return null;
    }
 
    public static Application getApp() {
        return application;
    }
 
    public static Resources getResources() {
        Context context = getAppContext();
        if (context != null) {
            return context.getResources();
        }
        return null;
    }
 
    public static void setSplashCls(Class cls) {
        ContextHelper.splashCls = cls;
    }
 
    public static Class getSplashCls() {
        return splashCls;
    }
 
    /**
     * 资源ID获取String
     */
    public static String getString(int stringId) {
        if (getAppContext() == null) {
            return Strings.EMPTY;
        }
        return getAppContext().getString(stringId);
    }
 
    public static String getString(int stringId, Object... formatArgs) {
        if (getAppContext() == null) {
            return Strings.EMPTY;
        }
        return getAppContext().getString(stringId, formatArgs);
    }
 
 
    public static int getDimensionPixelSize(int dimenId) {
        try {
            return getResources().getDimensionPixelSize(dimenId);
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }
 
    public static int getDimen(int dimenId) {
        try {
            return getResources().getDimensionPixelSize(dimenId);
        } catch (Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }
 
    /**
     * 获取应用的版本号
     */
    public static String getAppVersion() {
        Context context = getAppContext();
        if (context != null) {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo;
            try {
                packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
                return packageInfo.versionName;
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return Strings.EMPTY;
    }
 
    public static boolean isUsable(Context context) {
        if (context == null) {
            return false;
        }
 
        if (context instanceof Activity && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return !((Activity) context).isDestroyed();
        }
        return true;
    }
 
    public static void startAppSetting(){
        if(getAppContext()!=null) {
            Intent intent = new Intent("android.settings.SETTINGS");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getAppContext().startActivity(intent);
        }
    }
 
}