導航:首頁 > 操作系統 > 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設置全局變數相關的資料

熱點內容
健身房壓縮衣 瀏覽:973
單片機太陽光追蹤系統所需材料 瀏覽:356
比澤爾壓縮機型號規則 瀏覽:85
華興數控切斷編程 瀏覽:788
西安離心壓縮機 瀏覽:545
程序員需要優盤嗎 瀏覽:878
西藏掌上社保app在哪裡下載 瀏覽:599
怎麼讓伺服器固定 瀏覽:65
計數器定時器編程 瀏覽:13
程序員網上投資平台 瀏覽:877
用shell編程計算1加到100 瀏覽:233
外包公司的程序員一天寫多少代碼 瀏覽:532
蘋果手機主屏幕app如何移動 瀏覽:567
伺服器怎麼連接遠程密碼 瀏覽:431
linux娛樂命令 瀏覽:368
單片機數碼管循環顯示9到0 瀏覽:494
程序員懟代碼思路 瀏覽:327
新能源碼磚機產品介紹 瀏覽:37
模擬器共享里的文件夾名稱 瀏覽:840
easypanel控制面板源碼下載 瀏覽:532