在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>
运行一下看看结果。
分享到:
相关推荐
在Android中用textview实现简单的小说分页,字体可调节,未对字符串进行任何操作,分段和控制格式自行对字符串操作
浅谈Android中用自定义View实现折线图.pdf
在Android中用Fragment实现类似于微信或者QQ中的滑动切换界面的功能,并且给每一个Fragment绑定对应的活动。只是一个简单的样本,本人也才接触Android开发没多久,最近结合自己手头在尝试做的事情,实现的这种需求。...
用ViewPage实现像真实翻页那种效果,个人感觉效果特别好
android application development 中用到的源码-telephony-example,书中有分析,仅供参考
用CalendarView 实现的日历显示样式,可上下翻页
这是android application development 中用到的源码-dialing-example,在书中有详细的分析,大家看着用吧
Android中用xml配置旋转动画项目的要求说明.pdf 学习资料 复习资料 教学资源
主要给大家介绍了关于在java中用ObjectMapper类实现Json与bean转换的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
Android中用xml配置缩放动画的代码清单.pdf 学习资料 复习资料 教学资源
android中用到透明背景的小图标,很实用,如果你没有专门的美工的话,这些小图标,会使你的应用程序界面漂亮很多。
Android中用xml配置平移动画的代码清单.pdf 学习资料 复习资料 教学资源
Android中用ContentProvider快速查找通讯录信息的代码清单.pdf 学习资料 复习资料 教学资源
Android中用放大镜效果查看图库中的图的代码清单.pdf 学习资料 复习资料 教学资源
在Android Studio中用GreenDAO这个开源库写的简单代码
Android ViewPager实现仿QQ多页面滑动切换以及动画效果
pb中用DataWindow实现对多表的修改;pb中用DataWindow实现对多表的修改;pb中用DataWindow实现对多表的修改;pb中用DataWindow实现对多表的修改;pb中用DataWindow实现对多表的修改;pb中用DataWindow实现对多表的修改;pb...
解决android 与网站过程中不能传送中文的问题,用json传中文。