易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
首页
/
操作系统
/
Linux
/
Android中用Application类实现全局变量
在Java中如果要使用全局变量,一般定义public static类型的变量。但是这种方法不符合Android的框架架构,Android中要使用Application context。Application是一个基类,这个基类的作用是获取整个App的状态,我们需要自己定义一个类来继承这个基类。代码如下:
package
com.tianjf;
import
android.app.Application;
public
class
MyApplication
extends
Application {
private
boolean
mHasPassword;
public
boolean
ismHasPassword() {
return
mHasPassword;
}
public
void
setmHasPassword(
boolean
mHasPassword) {
this
.mHasPassword = mHasPassword;
}
@Override
public
void
onCreate() {
mHasPassword =
true
;
super
.onCreate();
}
}
我们定义了一个MyApplication继承自Application,并定义了一个全局变量mHasPassword,然后复写基类的onCreate方法,onCreate负责对所有全局变量赋初期值。我们还需要把自定义的Application类添加到AndroidManifest.xml里面,代码如下:
<application
android:name
=
"MyApplication"
。。。。。。。。。。。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。。。。。。。。。
</application>
这样做的目的:App的进程被创建的时候,这个类就会被实例化,onCreate方法就会被执行,给所有全局变量赋初期值。这样,所有的Activity就共同拥有这个类里面的变量了。下面用两个Activity来测试一下,当一个Activity改变了全局变量的值之后,看看另一个Activity能不能取到改变后的值。ApplicationDemoActivity.java
package
com.tianjf;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.view.View.OnClickListener;
public
class
ApplicationDemoActivity
extends
Activity
implements
OnClickListener {
private
static
final
String TAG =
"ApplicationDemoActivity"
;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.button).setOnClickListener(
this
);
}
@Override
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.button:
MyApplication myApplication = (MyApplication) getApplication();
Log.i(TAG, String.valueOf(myApplication.ismHasPassword()));
myApplication.setmHasPassword(
false
);
Intent intent =
new
Intent(
this
, AnotherActivity.
class
);
startActivity(intent);
break
;
default
:
break
;
}
}
}
AnotherActivity.java
package
com.tianjf;
import
android.app.Activity;
import
android.os.Bundle;
import
android.util.Log;
public
class
AnotherActivity
extends
Activity {
private
static
final
String TAG =
"AnotherActivity"
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.another);
MyApplication myApplication = (MyApplication) getApplication();
Log.i(TAG, String.valueOf(myApplication.ismHasPassword()));
}
}
main.xml
<?xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:orientation
=
"vertical"
>
<TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/hello"
/>
<Button
android:id
=
"@+id/button"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"Start another activity"
/>
</LinearLayout>
another.xml
<?xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:orientation
=
"vertical"
>
<TextView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"@string/hello"
/>
</LinearLayout>
运行一下看看结果。
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图