• android application类简单介绍(一)


    每次应用程序执行时。应用程序的application类保持实例化的状态。

    通过扩展applicaiton类,能够完毕下面3项工作:

    1.对android执行时广播的应用程序级事件如低低内做出响应。

    2.在应用程序组件之间传递对象(全局变量)。

    3.管理和维护多个应用程序组件使用的资源。

    当中,后两项工作通过使用单例类来完毕会更好。application会在创建应用程序进程的时候实例化。

    以下是扩展Application的演示样例代码:

    import android.app.Application;
    
    public class MyApplication extends Application {
    	private static MyApplication singleton;
    	//返回应用程序实例
    	public static MyApplication getInstance(){
    		return singleton;
    	}
    	@Override
    	public void onCreate() {
    		super.onCreate();
    		singleton = this;
    	}
    }
     在创建好自己的Application后,在mainfest里面的application注冊。例如以下:

    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:name="com.example.i18n.MyApplication"
            android:theme="@style/AppTheme" >

    至于get 和set :

    假如MyApplication有变量str,并提供getter和setter,例如以下:

    package com.example.i18n;
    
    import android.app.Application;
    
    public class MyApplication extends Application {
    	private static MyApplication singleton;
    	private String str;
    	//返回应用程序实例
    	public static MyApplication getInstance(){
    		return singleton;
    	}
    	@Override
    	public void onCreate() {
    		super.onCreate();
    		singleton = this;
    	}
    	public String getStr() {
    		return str;
    	}
    	public void setStr(String str) {
    		this.str = str;
    	}
    	
    }
    

    使用str和赋值:

    	MyApplication.getInstance().setStr("hello,bitch!");
    		String mystr = MyApplication.getInstance().getStr();
    		Log.e("str",mystr+"");


    先写到这里。

    晚安。

  • 相关阅读:
    Qt之xml文件解析
    Qt之connect
    Qt线程池
    电力电子PSIM仿真——PWM控制
    电力电子Simulink仿真——PWM控制
    电力电子Simulink仿真——直流直流
    电力电子Simulink仿真——整流电路
    电力电子Simulink仿真——电力电子器件
    信号与系统MATLAB仿真——LTI连续系统的时域分析
    信号与系统MATLAB仿真——信号及其运算
  • 原文地址:https://www.cnblogs.com/claireyuancy/p/7115181.html
Copyright © 2020-2023  润新知