Android开发–全局类的实现
对于一个Android应用来说,全局变量的使用是难免的,比如要保存一个用户登录之后的状态,而且这种变量对于应用来说是应该可以长时间保存的,Android对于这种变量的保存有自己自定义的全局类
原文:http://blog.youkuaiyun.com/qq_34861102/article/details/76922202
介绍
为了满足上述条件中的使用,以及平时的许多函数必要的初始化,我们可以自定义一个类继承于Application
的类
示例代码如下:package com.example.outlier.prictace_1; import android.annotation.TargetApi; import android.app.Application; import android.content.Context; import android.content.SharedPreferences; import android.os.Build; import io.rong.imkit.RongIM; import static io.rong.imkit.utils.SystemUtils.getCurProcessName; /** * Created by outlier on 2017/7/20. */ public class BaseApplication extends Application { private static String PREF_NAME = "yixing_app.pref"; static Context _context; private static boolean sIsAtLeastGB; static { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { sIsAtLeastGB = true; } } @Override public void onCreate() { super.onCreate(); _context = getApplicationContext(); /** * OnCreate 会被多个进程重入,这段保护代码,确保只有您需要使用 RongIM 的进程和 Push 进程执行了 init。 * io.rong.push 为融云 push 进程名称,不可修改。 */ if (getApplicationInfo().packageName.equals(getCurProcessName(getApplicationContext())) || "io.rong.push".equals(getCurProcessName(getApplicationContext()))) { //IMKit SDK调用第一步 初始化 RongIM.init(this); } } public static synchronized BaseApplication context() { return (BaseApplication) _context; } @TargetApi(Build.VERSION_CODES.GINGERBREAD) public static void apply(SharedPreferences.Editor editor) { if (sIsAtLeastGB) { editor.apply(); } else { editor.commit(); } } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static SharedPreferences getPreferences() { SharedPreferences pre = context().getSharedPreferences(PREF_NAME, Context.MODE_MULTI_PROCESS); return pre; } public static void set(String key, int value) { SharedPreferences.Editor editor = getPreferences().edit(); editor.putInt(key, value); apply(editor); } public static void set(String key, boolean value) { SharedPreferences.Editor editor = getPreferences().edit(); editor.putBoolean(key, value); apply(editor); } public static void set(String key, String value) { SharedPreferences.Editor editor = getPreferences().edit(); editor.putString(key, value); apply(editor); } public static boolean get(String key, boolean defValue) { return getPreferences().getBoolean(key, defValue); } public static String get(String key, String defValue) { return getPreferences().getString(key, defValue); } public static int get(String key, int defValue) { return getPreferences().getInt(key, defValue); } }
这里面可以自己来定义需要在每个
Application
需要初始化的函数比如://IMKit SDK调用第一步 初始化 RongIM.init(this);
然后对于系统长期保存的变量可以看到我们自己定一个
get
和set
的静态方法,通过静态方法的访问可以实现。这里是以最常见的键值对的形式存储
举个例子:
//登录的时候设定用户 BaseApplication.set("userid","123"); //访问 String userid = BaseApplication.get("userid","");
在任何一个Activity中设定之后在项目任何位置都可以进行直接的访问
- 完成编写之后⚠️
在AndroidMainfest.xml
中设定android:name=".BaseApplication"
:
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="RONG_CLOUD_APP_KEY"
android:value="0vnjpoad0eyrz" />