• Android用Application设置全局变量以及使用


    如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型;而在android中如果使用这样的全局变量就不符合Android的框架架构,但是可以使用一种更优雅的方式就是使用Application context。
      首先需要重写Application,主要重写里面的onCreate方法,就是创建的时候,初始化变量的值。然后在整个应用中的各个文件中就可以对该变量进行操作了。
      启动Application时,系统会创建一个PID,即进程ID,所有的Activity就会在此进程上运行。那么我们在Application创建的时候初始化全局变量,同一个应用的所有Activity都可以取到这些全局变量的值,换句话说,我们在某一个Activity中改变了这些全局变量的值,那么在同一个应用的其他Activity中值就会改变。下面举个例子详细介绍一下应用步骤。
    下面是MyApp.java

    package com.android.test; 
    import android.app.Application; 
    
    public class MyApp extends Application{ 
    
        private String mylabel ;     
        public String getLabel(){ 
            return mylabel; 
        }    
        public void setLabel(String s){ 
            this.mylabel = s; 
        } 
    
        @Override 
        public void onCreate() { 
            // TODO Auto-generated method stub 
            super.onCreate(); 
            setLabel("Welcome!"); //初始化全局变量        
        }    
    } 


    下面是mainActivity.java

    package com.ginwave.test; 
    
    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.util.Log; 
    
    public class mainActivity extends Activity { 
        
        private MyApp myApp; 
        
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            myApp = (MyApp) getApplication(); //获得自定义的应用程序MyApp 
            Log.i("guoll", "InitLabel:"+myApp.getLabel());   //将我们放到进程中的全局变量拿出来,看是不是我们曾经设置的值 
    
            myApp.setLabel("Changing!");  //修改一下 
            Log.i("guoll", "ChangeLabel:"+myApp.getLabel()); //看下,这个值改变了没有 
    
            Intent intent = new Intent();  //再看一下在另一个Activity中是取到初始化的值,还是取到修改后的值 
            intent.setClass(this, otherActivity.class); 
            startActivity(intent); 
        } 
    } 

    另一个otherActivity.java:

    package com.android.test; 
    
    import android.app.Activity; 
    import android.os.Bundle; 
    import android.util.Log; 
    
    public class otherActivity extends Activity{ 
        
        private MyApp myApp; 
        
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
    
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.main); 
                
                myApp = (MyApp) getApplication();  //获得自定义的应用程序MyApp 
                Log.i("guoll", "OhterActivity receive the Label:"+myApp.getLabel()); //查看变量值是否修改了 
    
        }        
    } 

    修改配置文件ApplicationManifest.xml,将要运行的应用程序MyApp加进去:

    <?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          package="com.android.test" 
          android:versionCode="1" 
          android:versionName="1.0"> 
        <!-- 在这里,将默认的Application设置成自己做的MyApp--> 
        <application android:name="MyApp" 
            android:icon="@drawable/icon" 
            android:label="@string/app_name" 
            > 
            <activity android:name=".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=".otherActivity"> 
            </activity> 
        </application> 
    
    </manifest> 
  • 相关阅读:
    各种地
    傅里叶变换
    点对点通信模型
    take office|boast|think twice|dispose of|level|stuff|'s mature for|a green hand|'s a slave to|
    resume|issue|transmit|sake|obliged|beyond her wildest dreams|echo|transmission|immense|consistent |convey to| boasted|satisfaction|rub|enrol|demonize
    but for|lest,in case和for fear (that)|confidential|item|adapted for|fee|debates| retain|substantial|proceeded to|refrain from|clear|perceive
    pressure to compete|listen to sb do|have sb do|felt like|shouldn't have done|spring up|in honour of|not more than|much as|
    the Uneducated are|anymore|that| so as to |die from|die of|
    the extent of|fill in|find itimpossible to|something|the other day|Be man enough to do sth/for sth|cure sb of |draw out| gone over|made for | see someone off,|
    when|nobody|hazard|lane|circuit|
  • 原文地址:https://www.cnblogs.com/xuewater/p/2646431.html
Copyright © 2020-2023  润新知