Android的开发-全局类的实现
对于一个Android的应用来说,全局变量的使用是难免的,比如要保存一个用户登录之后的状态,而且这种变量对于应用来说是应该可以长时间保存的,机器人对于这种变量的保存有自己自定义的全局类。
介绍
为了满足上述条件中的使用,平时以及的许多函数必要的初始化,我们可以自定义一个类继承于Application的类
示例代码如下:
| 1 2 3 4 五 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 三十 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 | <code>packagecom.example.outlier.prictace_1;<font></font><font></font>importandroid.annotation.TargetApi;<font></font>importandroid.app.Application;<font></font>importandroid.content.Context;<font></font>importandroid.content.SharedPreferences;<font></font>importandroid.os.Build;<font></font><font></font>importio.rong.imkit.RongIM;<font></font><font></font>importstaticio.rong.imkit.utils.SystemUtils.getCurProcessName;<font></font><font></font>/**<font></font> * Created by outlier on 2017/7/20.<font></font> */<font></font><font></font>publicclassBaseApplication  extendsApplication {<font></font>    privatestaticString PREF_NAME = "yixing_app.pref";<font></font><font></font><font></font>    staticContext _context;<font></font>    privatestaticbooleansIsAtLeastGB;<font></font><font></font>    static{<font></font>        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {<font></font>            sIsAtLeastGB = true;<font></font>        }<font></font><font></font>    }<font></font><font></font>    @Override<font></font>    publicvoidonCreate() {<font></font>        super.onCreate();<font></font>        _context = getApplicationContext();<font></font>        /**<font></font>         * OnCreate 会被多个进程重入,这段保护代码,确保只有您需要使用 RongIM 的进程和 Push 进程执行了 init。<font></font>         * io.rong.push 为融云 push 进程名称,不可修改。<font></font>         */<font></font>        if(getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext()))<font></font>                || "io.rong.push".equals(getCurProcessName(getApplicationContext()))) {<font></font>            //IMKit SDK调用第一步 初始化<font></font>            RongIM.init(this);<font></font>        }<font></font><font></font><font></font>    }<font></font><font></font>    publicstaticsynchronizedBaseApplication context() {<font></font>        return(BaseApplication) _context;<font></font>    }<font></font><font></font>    @TargetApi(Build.VERSION_CODES.GINGERBREAD)<font></font>    publicstaticvoidapply(SharedPreferences.Editor editor) {<font></font>        if(sIsAtLeastGB) {<font></font>            editor.apply();<font></font>        } else{<font></font>            editor.commit();<font></font>        }<font></font>    }<font></font><font></font>    @TargetApi(Build.VERSION_CODES.HONEYCOMB)<font></font>    publicstaticSharedPreferences getPreferences() {<font></font>        SharedPreferences pre = context().getSharedPreferences(PREF_NAME,<font></font>                Context.MODE_MULTI_PROCESS);<font></font>        returnpre;<font></font>    }<font></font><font></font><font></font><font></font>    publicstaticvoidset(String key, intvalue) {<font></font>        SharedPreferences.Editor editor = getPreferences().edit();<font></font>        editor.putInt(key, value);<font></font>        apply(editor);<font></font>    }<font></font><font></font>    publicstaticvoidset(String key, booleanvalue) {<font></font>        SharedPreferences.Editor editor = getPreferences().edit();<font></font>        editor.putBoolean(key, value);<font></font>        apply(editor);<font></font>    }<font></font><font></font>    publicstaticvoidset(String key, String value) {<font></font>        SharedPreferences.Editor editor = getPreferences().edit();<font></font>        editor.putString(key, value);<font></font>        apply(editor);<font></font>    }<font></font><font></font><font></font><font></font>    publicstaticbooleanget(String key, booleandefValue) {<font></font>        returngetPreferences().getBoolean(key, defValue);<font></font>    }<font></font><font></font>    publicstaticString get(String key, String defValue) {<font></font>        returngetPreferences().getString(key, defValue);<font></font>    }<font></font><font></font>    publicstaticintget(String key, intdefValue) {<font></font>        returngetPreferences().getInt(key, defValue);<font></font>    }<font></font><font></font>}</code> | 
这里面可以自己来定义需要在每个Application需要初始化的函数比如:
| 1 2 | <code><code>//IMKit SDK调用第一步 初始化RongIM.init(this);</code></code> | 
然后对于系统长期保存的变量可以看到我们自己定一个get和set的静态方法,通过静态方法的访问可以实现。
这里是以最常见的键值对的形式存储
举个例子:
| 1 2 3 4 | <code><code><code><code>        //登录的时候设定用户        BaseApplication.set("userid","123");        //访问        String userid = BaseApplication.get("userid","");</code></code></code></code> | 
在任何一个Activity中设定之后在项目任何位置都可以进行直接的访问
- 完成编写之后??
在AndroidMainfest.xml中设定android:name=".BaseApplication":
 
                    
                 
