Android-設置全局變量


public class BaseContext extends Application {
    private static Context mContext;
 
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
    }
    public static Context getContext(){
        return mContext;
    }

最後在清單文件的Application中加入一個name屬性:
例如:
android:name=".utils.BaseContext"

在需要調用的方法寫:

BaseContext.getContext()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章