如何在应用程序的生命周期中创建全局变量,而不管哪个活动正在运行。
当前回答
import android.app.Application;
public class Globals extends Application
{
private static Globals instance = null;
private static int RecentCompaignID;
private static int EmailClick;
private static String LoginPassword;
static String loginMemberID;
private static String CompaignName = "";
private static int listget=0;
//MailingDetails
private static String FromEmailadd="";
private static String FromName="";
private static String ReplyEmailAdd="";
private static String CompaignSubject="";
private static int TempId=0;
private static int ListIds=0;
private static String HTMLContent="";
@Override
public void onCreate()
{
super.onCreate();
instance = this;
}
public static Globals getInstance()
{
return instance;
}
public void setRecentCompaignID(int objRecentCompaignID)
{
RecentCompaignID = objRecentCompaignID;
}
public int getRecentCompaignID()
{
return RecentCompaignID;
}
public void setLoginMemberID(String objloginMemberID)
{
loginMemberID = objloginMemberID;
}
public String getLoginMemberID()
{
return loginMemberID;
}
public void setLoginMemberPassword(String objLoginPassword)
{
LoginPassword = objLoginPassword;
}
public String getLoginMemberPassword()
{
return LoginPassword;
}
public void setEmailclick(int id)
{
EmailClick = id;
}
public int getEmailClick()
{
return EmailClick;
}
public void setCompaignName(String objCompaignName)
{
CompaignName=objCompaignName;
}
public String getCompaignName()
{
return CompaignName;
}
public void setlistgetvalue(int objlistget)
{
listget=objlistget;
}
public int getlistvalue()
{
return listget;
}
public void setCompaignSubject(String objCompaignSubject)
{
CompaignSubject=objCompaignSubject;
}
public String getCompaignSubject()
{
return CompaignSubject;
}
public void setHTMLContent(String objHTMLContent)
{
HTMLContent=objHTMLContent;
}
public String getHTMLContent()
{
return HTMLContent;
}
public void setListIds(int objListIds)
{
ListIds=objListIds;
}
public int getListIds()
{
return ListIds;
}
public void setReplyEmailAdd(String objReplyEmailAdd)
{
ReplyEmailAdd=objReplyEmailAdd;
}
public String getReplyEmailAdd()
{
return ReplyEmailAdd;
}
public void setFromName(String objFromName)
{
FromName=objFromName;
}
public String getFromName()
{
return FromName;
}
public void setFromEmailadd(String objFromEmailadd)
{
FromEmailadd=objFromEmailadd;
}
public String getFromEmailadd()
{
return FromEmailadd;
}
}
其他回答
试着这样做:
创建共享数据类:
SharedData.java
import android.app.Application;
/**
* Created by kundan on 6/23/2015.
*/
public class Globals {
private static Globals instance = new Globals();
// Getter-Setters
public static Globals getInstance() {
return instance;
}
public static void setInstance(Globals instance) {
Globals.instance = instance;
}
private String notification_index;
private Globals() {
}
public String getValue() {
return notification_index;
}
public void setValue(String notification_index) {
this.notification_index = notification_index;
}
}
在那些你想要设置/获取数据的类中声明/初始化一个类的全局实例(在onCreate()方法之前使用此代码)
Globals sharedData = Globals.getInstance();
组数据:
sharedData.setValue("kundan");
得到的数据:
String n = sharedData.getValue();
你可以扩展基本的android.app.Application类,并像这样添加成员变量:
public class MyApplication extends Application {
private String someVariable;
public String getSomeVariable() {
return someVariable;
}
public void setSomeVariable(String someVariable) {
this.someVariable = someVariable;
}
}
在你的android manifest中,你必须声明实现android.app. application的类(添加android:name="。属性到现有的应用程序标签):
<application
android:name=".MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name">
然后在你的活动中,你可以像这样获取和设置变量:
// set
((MyApplication) this.getApplication()).setSomeVariable("foo");
// get
String s = ((MyApplication) this.getApplication()).getSomeVariable();
简单! !
那些你想作为全局变量访问的变量,你可以声明为静态变量。现在,你可以通过
classname.variablename;
public class MyProperties {
private static MyProperties mInstance= null;
static int someValueIWantToKeep;
protected MyProperties(){}
public static synchronized MyProperties getInstance(){
if(null == mInstance){
mInstance = new MyProperties();
}
return mInstance;
}
}
MyProperites.someValueIWantToKeep;
Thats It !;)
这个全局变量适用于我的项目:
public class Global {
public static int ivar1, ivar2;
public static String svar1, svar2;
public static int[] myarray1 = new int[10];
}
// How to use other or many activity
Global.ivar1 = 10;
int i = Global.ivar1;
使用SharedPreferences存储和检索全局变量。
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String userid = preferences.getString("userid", null);
推荐文章
- 模块是用不兼容的Kotlin版本编译的。其元数据的二进制版本为1.5.1,预期版本为1.1.15
- 如何在Android工作室添加“libs”文件夹?
- 使用什么api来绘制其他应用程序(如Facebook的Chat Heads)?
- getDefaultSharedPreferences和getSharedPreferences的区别
- 如何模拟按钮点击使用代码?
- Android Webview给出net::ERR_CACHE_MISS消息
- Parcelable遇到IOException写入序列化对象getactivity()
- 如何在Android中动态更改菜单项文本
- 如何将Base64字符串转换为位图图像,以显示在一个ImageView?
- 新版本的Android模拟器问题-模拟器进程已终止
- 没有与请求版本匹配的NDK版本
- 如何将一个颜色整数转换为十六进制字符串在Android?
- 格式浮动到小数点后n位
- 移除一个onclick监听器
- 如何圆形图像与滑翔图书馆?