如何从静态上下文中获取资源内容?
2022-08-31 06:40:51
我想在做其他任何事情(如在小部件上)之前从文件中读取字符串,那么如果没有要调用的活动对象,我该怎么做呢?xmlsetTextgetResources()
我想在做其他任何事情(如在小部件上)之前从文件中读取字符串,那么如果没有要调用的活动对象,我该怎么做呢?xmlsetTextgetResources()
应用程序的子类public class App extends Application {
android:name<application>AndroidManifest.xmlandroid:name=".App"
onCreate()thismContextgetContext()这是它应该看起来的样子:
public class App extends Application{
private static Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public static Context getContext(){
return mContext;
}
}
现在你可以使用:每当你想得到一个上下文,然后(或)。App.getContext()getResources()App.getContext().getResources()
仅适用于系统资源!
用
Resources.getSystem().getString(android.R.string.cancel)
您可以在应用程序中的任何位置使用它们,甚至在静态常量声明中也是如此!