Android开发--全局类的实现(用于保存使用的全局变量)

本文介绍了在Android应用中实现全局变量的方法,通过创建一个继承自Application的自定义类,保存长期有效的用户状态和其他初始化所需数据。示例代码展示了如何定义静态方法以实现键值对形式的数据存储,并在任意Activity中访问这些全局变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

    然后对于系统长期保存的变量可以看到我们自己定一个getset的静态方法,通过静态方法的访问可以实现。

    这里是以最常见的键值对的形式存储

    举个例子:

            //登录的时候设定用户
            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" />
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值