导航:首页 > 操作系统 > android设置全局变量

android设置全局变量

发布时间:2022-07-16 19:40:46

androidstudio怎么把变量提为全局

千锋扣丁学堂Android开发为您解答:
1、使用application来保存全局变量
这里没有太多理论性的东西,无非就是一些实际操作。
1.1定义Data类继承Application Data.class
1.2在manifest.xml中声明application
1.3创建两个Activity
MainActivity.class、secondActivity.class
2、使用普通的类Data.class来保存全局变量
1.1 定义Data.class
1.2创建两个Activity
MainActivity.class、secondActivity.class

⑵ 在Android中如何使用全局变量--Application context (转)

可以将变量存放在Application中,Context,中文直译为“上下文”,SDK中对其说明如下:
Interface to global information about an application environment. This is an abstract class whose implementation
is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls
for application-level operations such as launching activities, broadcasting and receiving intents, etc。
从上可知一下三点即:

1、它描述的是一个应用程序环境的信息,即上下文。
2、该类是一个抽象(abstract class)类,Android提供了该抽象类的具体实现类(后面我们会讲到是ContextIml类)。
3、通过它我们可以获取应用程序的资源和类,也包括一些应用级别操作,例如:启动一个Activity,发送广播,接受Intent信息等。

以下为使用Application存储全局变量的示例代码:

1.继承Application,并在Application里声明全局变量。
public class MyApplication extends Application {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}

2.在AndroidManifest.xml的application节点中声明这个Application。
<application android:name="com.xxx.xxx.MyApplication">

3.在Activity中获取Application对象,并存取全局变量。
User user = new User();
user.setUserName("example");
MyrApplication app= (MyApplication ) getApplicationContext();
app.setUser(user); //将变量存到application
User tmp = app.getUser();//从application中读取全局变量。

javaAndroid开发,如何定义全局变量

自定义一个类继承Application,fontFace作为一个静态变量放在Application里,重写自定义Application类的onCreate方法,在里面初始化fontFace变量,最后记得在AndroidManifest里注册自定义的Application类
引用的时候用Application类名.fontFace就可以了

⑷ android如何让系统库成为全局

一、通过Settings.System进行读写
//其中"getXXX"代表对应的类似方法,如getInt()、getBoolean、putString()等。
//通过变量名称获取值,如果变量不存在,数据库中没有设置过初始值或者该值类型不对,将抛出SettingNotFoundException异常
Settings.System.getXXX(ContentResolver cr, String name);
//通过变量名称获取值,如果发生上面方法中导致异常的情况,将返给定的默认值
Settings.System.getXXX(ContentResolver cr, String name, XXX def);
//将指定名称的值写入数据库
Settings.System.putXXX(ContentResolver cr,String name, XXX Value);
非系统权限,需要在App项目的AndroidMainfes.xml文件中添加如下权限:

<uses-permission android:name="android.permission.READ_SETTINGS" /><uses-permission android:name="android.permission.WRITE_SETTINGS" />
二、在Settings.System添加一个自定义的全局变量
Settings.java文件位于frameworks\base\core\java\android\provider下,打开该文件,搜索关键词 SETTINGS_TO_BACKUP ,共有两处,一处是在Settings里面,另一处在内部类Settings.System里面,在SETTINGS_TO_BACKUP数组上面添加自定义变量,同时在该数组里面添加自定义变量名称,Settting和内部类System都需要添加(共四个地方),比如自定义系统变量SYSTEM_ZWH:
public static final String SYSTEM_ZWH = "system_zwh"; Public static final String[] SETTINGS_TO_BACKUP = { ... SYSTEM_ZWH, ... }
在代码中我们就可以通过对于的get和put方法对该值进行读取和写入操作了。

⑸ android使用全局变量.求解答@慈税。

⑹ android中怎么设置全局变量

呵呵 我的问题没有说清楚,补充下:
public class CAppData extends Application
{
public int a = 0;
}
要再Activity中石油CAppData中的数据是不是只能在Activity的onCreate()方法中使用

⑺ android 如何定义全局变量

找到一个和我有类似需求的问题,其下给出了不错的解决方案,也正是我之前想到的,这种方法貌似很方便。 The more general problem you are encountering is how to save stateacross several Activities and all parts of your application. A staticvariable (for instance, a singleton) is a common Java way of achievingthis. I have found however, that a more elegant way in Android is toassociate your state with the Application context. --如想在整个应用中使用,在java中一般是使用静态变量,而在android中有个更优雅的方式是使用Application context。 As you know, each Activity is also a Context, which is informationabout its execution environment in the broadest sense. Your applicationalso has a context, and Android guarantees that it will exist as asingle instance across your application. --每个Activity 都是Context,其包含了其运行时的一些状态,android保证了其是single instance的。 The way to do this is to create your own subclass of android.app.Application,and then specify that class in the application tag in your manifest.Now Android will automatically create an instance of that class andmake it available for your entire application. You can access it fromany context using the Context.getApplicationContext() method (Activityalso provides a method getApplication() which has the exact sameeffect): --方法是创建一个属于你自己的android.app.Application的子类,然后在manifest中申明一下这个类,这是 android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。

⑻ 在Android中如何使用全局变量

关于android中是否可以使用全局变量,当然可以。做Java的人肯定都用过全局变量了 ,使用方法无非是定义一个静态变量,public类型,这样在其他类中就可以直接调用了

⑼ android如何从本地变量存储在全局变量

Android提供了一个类似于ServletContext的全局变量,叫Application。可以利用它存储一些全局变量!

示例:
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import android.app.Application;

public class MyApplication extends Application {
private Map<String, Object> mData;

public Map<String, Object> getmData() {
return mData;
}

@Override
public void onCreate() {
super.onCreate();

mData = new HashMap<String, Object>();
//synchronized the map
mData = Collections.synchronizedMap(mData);

// then restore your map
}

public void onTerminate() {
super.onTerminate();
//save data of the map
}
}

然后在AndroidManifest里面配置<application>节点的属性

<application android:name=".MyApplication">

阅读全文

与android设置全局变量相关的资料

热点内容
本田App怎么连接爱车 浏览:775
男士买衣服在哪个app实惠 浏览:690
安卓车机怎么颜色反转 浏览:901
手机uc下载的文件夹 浏览:962
程序员评论南京 浏览:88
冠道怎么连接安卓车载 浏览:317
手机怎么把两张图片做成文件夹 浏览:721
抖音导出表格发货加密 浏览:133
自己电脑怎么模拟成服务器 浏览:553
单片机的Vpp是 浏览:351
iua编译器下载官方 浏览:85
压缩机高低压快速平衡 浏览:875
phpai 浏览:708
怎么不被命令 浏览:87
大话缘定三生服务器什么便宜 浏览:968
idea编译内部类 浏览:467
pdf2word在线转换 浏览:588
tim储存在哪个文件夹 浏览:623
华硕电脑u盘加密最简单方法 浏览:854
编程过路马游戏 浏览:610