歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android Application 創建全局變量

Android Application 創建全局變量

日期:2017/3/1 10:23:22   编辑:Linux編程

以前都是建立一個ConstData的類來保存全局用的變量,但是有時候確實是有點小問題。

所以研究了一下使用Application來建立全局變量,下面就是代碼,主要分為四個文件:

(1)是MyApplication類,保存全局變量以及變量的查詢和修改

(2)TestAndroid 類 也是主類

(3)otherActivity 另外一個類調用全局變量試試是不是被主類改變了

(4)manifest.xml文件

MyApplication

  1. package an.test.android;
  2. import android.app.Application;
  3. public class MyApp extends Application{
  4. private String mylabel ;
  5. public String getLabel(){
  6. return mylabel;
  7. }
  8. public void setLabel(String s){
  9. this.mylabel = s;
  10. }
  11. @Override
  12. public void onCreate() {
  13. // TODO Auto-generated method stub
  14. super.onCreate();
  15. setLabel("Welcome!"); //初始化全局變量
  16. }
  17. }
TestAndroid
  1. package an.test.android;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. public class TestAndroid extends Activity {
  7. /** Called when the activity is first created. */
  8. private MyApp myApp;
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.main);
  12. myApp = (MyApp) getApplication(); //獲得自定義的應用程序MyApp
  13. Log.i("guoll", "InitLabel:"+myApp.getLabel()); //將我們放到進程中的全局變量拿出來,看是不是我們曾經設置的值
  14. myApp.setLabel("Changing!"); //修改一下
  15. Log.i("guoll", "ChangeLabel:"+myApp.getLabel()); //看下,這個值改變了沒有
  16. Intent intent = new Intent(); //再看一下在另一個Activity中是取到初始化的值,還是取到修改後的值
  17. intent.setClass(this, otherActivity.class);
  18. startActivity(intent);
  19. }
  20. }
otherActivity
  1. package an.test.android;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. public class otherActivity extends Activity{
  6. private MyApp myApp;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. myApp = (MyApp) getApplication(); //獲得自定義的應用程序MyApp
  12. Log.i("guoll", "OhterActivity receive the Label:"+myApp.getLabel()); //查看變量值是否修改了
  13. }
  14. }
manifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="an.test.android"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name" android:name="MyApp" >
  7. <activity android:name=".TestAndroid"
  8. android:label="@string/app_name">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <activity android:name=".otherActivity">
  15. </activity>
  16. </application>
  17. </manifest>
這裡尤其要注意的一點就是:MyApp這個類要在manifest中進行注冊,其實就是加上了一行代碼

android:name="MyApp"

但是這個是必須的!

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved