android定義全局變數
㈠ Android 開機定義全局變數問題
沒有太明白你說的是什麼意思
這是個開機廣播,你可以將在這里需要用到的變數值都傳出去的,類似你收到開機廣播後,跳轉開啟你應用的service或者直接打開你的應用。
㈡ 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如何讓系統庫成為全局
一、通過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中如何使用全局變數--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中gradle文件中${ } 全局變數在哪裡定義的
為方便在不同mole中設置版本號等配置信息,可以通過配置全局變數來統一所有mole的公共配置信息。
設置方法一般分為兩種:
一、獨立文件配置
1.1.在項目的根目錄下新建config.gradle文件
1.2.將gradle中的公共信息寫入config.gradle文件中:
1.3.在主項目的build.gradle中申明一下:
1.4.在項目中引用我們的路徑配置。如下圖:
二、在gradle.properties或者local.properties文件中配置
如下為簽名配置:
看了覺得也還挺方便的,但是!需要注意的是:因為用到的都是String變數,當需要到Integer變數時,就麻煩點了:
需要用Integer.parseInt();方法對String類型進行轉換!
㈦ 在Android中如何使用全局變數
關於android中是否可以使用全局變數,當然可以。做Java的人肯定都用過全局變數了 ,使用方法無非是定義一個靜態變數,public類型,這樣在其他類中就可以直接調用了
㈧ android Application全局變數
不是啊,你聲明在類裡面而不是onCreate方法裡面就可以在這個Activity中使用。
public class GuessNumberActivity extends Activity {
Button btn1 = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.btn1);
}
//在其他函數中使用
bt1.setOnClickListener(new Button.onClickListener(){.........});