1、全局對象是Activity之間傳遞數據的一種比較實用的方式,比如在JavaWeb中有四個作用域,這四個作用域從小到大分別是Page、Request、Session和Application,其中Application域在應用程序的任何地方都可以使用和訪問,除非是Web服務器停止。Android中的全局對象非常類似于JavaWeb中的Application域,只要Android應用程序不清除內存,全局對象就可以一直訪問~
2、新建一個Android項目:“android_app”,進入“main.xml”添加一個Button,代碼如下:
~~~
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="使用Application傳遞數據" />
~~~
3、在當前目錄下創建一個“other.xml”,添加一個TextView,代碼如下:
~~~
<TextText
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextText>
~~~
4、在當前包下新建一個類“OtherActivity”,并使其繼承Activity;添加一個“onCreate”方法,代碼如下:
~~~
package com.android.app;
import android.app.Activity;
import android.os.Bundle;
public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
}
}
~~~
5、為了能在程序中使用全局變量,需要在當前包下新建一個普通類“MyApp”,使其繼承“Application”,隨后聲明一個“name”成員并提供構造方法和添加“onCreate”代碼如下:
~~~
package com.android.app;
import android.app.Application;
public class MyApp extends Application {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
setName("張三");
}
}
~~~
6、進入“Main.java”,添加Button和MyApp成員,隨后為Button設置一個點擊的事件,代碼如下:
~~~
package com.android.app;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
private Button button;
private MyApp myApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button);
// 為button注冊一個點擊事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myApp = (MyApp) getApplication();
myApp.setName("Jack");// 修改之后的名字
Intent intent = new Intent(Main.this, OtherActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
~~~
7、進入“OtherActivity.java”中,聲明一個MyApp成員,并獲取myApp,代碼如下:
~~~
package com.android.app;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity {
private MyApp myApp;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView = (TextView) this.findViewById(R.id.msg);
myApp = (MyApp) getApplication();
textView.setText("-appname-->>" + myApp.getName());
}
}
~~~
8、修改清單文件“AndroidManifest.xml”,在“Application”標簽中新加入一個屬性“name”,再加入一個“Activity”,代碼如下:
~~~
<application
android:name=".MyApp"//后添加的屬性
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.android.app.Main"
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>
~~~
9、運行一下,得到結果:


- 前言
- Android應用程序剖析
- (一)——生命周期
- (二)——使用Intent傳數據之通用方式
- (三)——使用靜態變量傳遞數據
- (四)——通過剪切板傳遞數據
- (五)——通過全局變量傳遞數據
- (六)——從Activity返回數據
- adt-bundle-linux-x86_64-20131030下新建工程提示找不到adb和R.java問題的解決
- Eclipse啟動時提示fail to create the Java Virtual Machine問題的解決
- Android常見UI組件之ListView(一)
- Android常見UI組件之ListView(二)——定制ListView
- (七)——顯示對話框窗口
- (八)——顯示進度對話框
- (九)——更復雜的進度對話框
- (十)——使用意圖鏈接活動
- (十一)——從意圖返回結果
- (十二)——使用意圖傳遞數據的幾種方式
- (十三)——碎片(一)
- (十四)——在運行時添加碎片(附源碼)
- (十五)——碎片的生命周期(附源碼)
- (十六)——碎片之間進行交互(附源碼)
- (十七)——使用意圖調用內置應用程序
- (十八)——使用意圖篩選器和實現瀏覽網頁(附源碼)