• [Android学习笔记]扩展application


    扩展Application对象

    每一个应用程序启动之后,都会分配一个linux用户,并且运行在一个独立的进程中。
    默认情况下,一个应用程序只会运行在一个进程中(可以通过配置android:process分配到不同的进程中),没一个进程都会有一个独立的的应用程序对象.
    通过自定义应用程序对象,可以在当前应用程序环境中方便的实现数据共享,而不必通过Intent把大量数据在不同的Activity中来回传递


    一般步骤为:
    1.自定义Application类
    2.修改AnroidManifest.xml,将自定义的Application类作为该应用程序的应用程序环境对象
    3.使用自定义的Application对象


    ex:

    1.扩展Application

    /**
     * 扩展Application,定义自己的Application
     */
    public class myApplication extends Application
    {
        public int myData = 1000;
        
        // 定义个单例,可以在任何地方获取到应用程序环境对象 , 与在Activity中调用getApplication()一致
        private static myApplication instance;
        public static myApplication getInstance()
        {
            return instance;
        }
    
        @Override
        public void onCreate()
        {
            // TODO Auto-generated method stub
            // super.onCreate();
            
            instance = this;
        }
    }

    2.修改配置文件,使用自定义的Application作为应用的应用程序环境对象(<application>项中添加android:name="包.myApplication")

    <application
            android:name="com.example.context.myApplication"
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.broadcastreceivertest.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name="com.example.broadcastreceivertest.SecondActivity"
                android:label="@string/title_activity_second" >
            </activity>
        </application>
  • 相关阅读:
    Java使用printf格式化日期
    Java时间Date类
    Java数组
    Spring Cloud Stream
    Spring Cloud Bus
    Spring Cloud Config
    api服务网关?
    SPRINGBOOT集成SWAGGER2
    MySQL锁(一)全局锁:如何做全库的逻辑备份?
    Spring的FactoryBean
  • 原文地址:https://www.cnblogs.com/hellenism/p/3770828.html
Copyright © 2020-2023  润新知