首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从静态上下文中获取资源内容?

如何从静态上下文中获取资源内容?
EN

Stack Overflow用户
提问于 2010-12-09 04:03:08
回答 11查看 141.1K关注 0票数 178

在对小部件执行其他操作(如setText )之前,我希望从xml文件中读取字符串,那么如果没有activity对象来调用getResources(),我如何做到这一点呢?

EN

回答 11

Stack Overflow用户

发布于 2019-10-30 22:36:24

我的Kotlin解决方案是使用静态应用程序上下文:

代码语言:javascript
复制
class App : Application() {
    companion object {
        lateinit var instance: App private set
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
    }
}

还有Strings类,我在任何地方都会用到:

代码语言:javascript
复制
object Strings {
    fun get(@StringRes stringRes: Int, vararg formatArgs: Any = emptyArray()): String {
        return App.instance.getString(stringRes, *formatArgs)
    }
}

因此,您可以以一种干净的方式获取资源字符串

代码语言:javascript
复制
Strings.get(R.string.some_string)
Strings.get(R.string.some_string_with_arguments, "Some argument")

请不要删除此答案,让我保留一个。

票数 21
EN

Stack Overflow用户

发布于 2017-02-12 17:07:18

还有另一种可能性。我从如下资源加载OpenGl着色器:

代码语言:javascript
复制
static private String vertexShaderCode;
static private String fragmentShaderCode;

static {
    vertexShaderCode = readResourceAsString("/res/raw/vertex_shader.glsl");
    fragmentShaderCode = readResourceAsString("/res/raw/fragment_shader.glsl");
}

private static String readResourceAsString(String path) {
    Exception innerException;
    Class<? extends FloorPlanRenderer> aClass = FloorPlanRenderer.class;
    InputStream inputStream = aClass.getResourceAsStream(path);

    byte[] bytes;
    try {
        bytes = new byte[inputStream.available()];
        inputStream.read(bytes);
        return new String(bytes);
    } catch (IOException e) {
        e.printStackTrace();
        innerException = e;
    }
    throw new RuntimeException("Cannot load shader code from resources.", innerException);
}

如您所见,您可以访问path /res/...中的任何资源,并将aClass更改为您的类。这也是我在测试中加载资源的方式(androidTests)

票数 6
EN

Stack Overflow用户

发布于 2015-09-25 19:45:03

单例:

代码语言:javascript
复制
package com.domain.packagename;

import android.content.Context;

/**
 * Created by Versa on 10.09.15.
 */
public class ApplicationContextSingleton {
    private static PrefsContextSingleton mInstance;
    private Context context;

    public static ApplicationContextSingleton getInstance() {
        if (mInstance == null) mInstance = getSync();
        return mInstance;
    }

    private static synchronized ApplicationContextSingleton getSync() {
        if (mInstance == null) mInstance = new PrefsContextSingleton();
        return mInstance;
    }

    public void initialize(Context context) {
        this.context = context;
    }

    public Context getApplicationContext() {
        return context;
    }

}

在您的Application子类中初始化单例:

代码语言:javascript
复制
package com.domain.packagename;

import android.app.Application;

/**
 * Created by Versa on 25.08.15.
 */
public class mApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        ApplicationContextSingleton.getInstance().initialize(this);
    }
}

如果我没记错的话,这给了你一个到处applicationContext的钩子,用ApplicationContextSingleton.getInstance.getApplicationContext();调用它你不需要在任何时候清除它,因为当应用程序关闭时,无论如何这都会伴随着它。

记住要更新AndroidManifest.xml以使用这个Application子类:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.domain.packagename"
    >

<application
    android:allowBackup="true"
    android:name=".mApplication" <!-- This is the important line -->
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:icon="@drawable/app_icon"
    >

现在你应该可以在任何地方使用ApplicationContextSingleton.getInstance().getApplicationContext().getResources()了,应用程序子类不能使用的地方也很少。

如果你发现这里有什么问题,请告诉我,谢谢。:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4391720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档