今天在学习Activity使用全局变量传递数据时遇到了许多问题,现在总结一下:
使用J2EE的读者来说都知道Java Web的四个作用域,这四个作用域从小到大分别是:Page、Request、Session和Application,其中Application域在应用程序的任何地方都可以使用和访问,除非是Web服务器停止,Android中的全局对象非常类似于Java Web中的Application域,除非是Android应用程序清除内存,否则全局对象将一直可以访问。
这是我工程的截图
主方法:Main.java
package com.android.life;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
private Button button;
private MyApp myApp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)this.findViewById(R.id.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);
}
});
}
}
OtherActivity.java
package com.android.life;
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());
}
}
MyApp.java
package com.android.life;
import android.app.Application;
public class MyApp extends Application {
public 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("张三");
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/button" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="使用Application传递数据" />
</LinearLayout>
other.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/msg"/> </LinearLayout>
strings.xml
<resources>
<string name="hello">Hello World,Main</string>
<string name="app_name">android 的生命周期演示</string>
</resources>
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.life"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:name=".MyApp"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
>
<activity android:name=".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>
</manifest>
如果出现类转换失败:java.lang.ClassCastException: android.app.Application
记得在清单文件中加上MyApp的声明,如上红字描述。