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

熱點內容
程序員可以開網店嗎 瀏覽:115
演算法工程師面試經驗 瀏覽:686
有什麼好用的陪聊app 瀏覽:698
什麼是備中心伺服器 瀏覽:144
linux配置本地yum源 瀏覽:539
半導體器件與工藝pdf 瀏覽:528
超大文件解壓太慢 瀏覽:861
微光app主頁的愛心代表什麼意思 瀏覽:563
程序員和餃子做飯 瀏覽:307
美團app的點擊騎車在哪裡 瀏覽:723
程序員標配條件 瀏覽:211
免費電腦解壓app排行榜前十名 瀏覽:189
順序表查找演算法 瀏覽:463
整合包解壓後是亂碼 瀏覽:300
xp系統如何查找伺服器名 瀏覽:983
土的壓縮系數的確定方法 瀏覽:647
程序員家裡健身 瀏覽:620
電視看籃球app哪個好 瀏覽:47
高中畢業當程序員 瀏覽:245
php標簽屬性大全 瀏覽:897