Android 如何从主要活动之外读取资产

发布于 2024-10-01 08:46:35 字数 160 浏览 1 评论 0原文

我需要能够从应用程序的主要活动之外调用 readAsset。我听人们提到需要传递上下文,但语言非常模糊。有人可以描述将调用 readAsset 的功能添加到非主要活动的现有类所需的步骤吗?在主活动中创建一个公共函数并让其他人调用该函数将不起作用,因为我需要添加 readAsset 的地方位于单独的线程中。

I need to be able to be able to call readAsset from outside of the main activity of my application. I have heard people mention needing to pass the Context around, but the language has been very vague. Can someone describe the steps necessary to add the ability to call readAsset to an existing class that is not the main activity? Creating a public function in the main activity and having others call that will not work as the place I need to add readAsset to, is in a separate thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她说她爱他 2024-10-08 08:46:35
public class NonActivity {
    public void doStuff(Context c) {
        //read from assets
        c.getAssets();
        //use assets however
    }
}

不确定你在问什么,但也许是这样的?只需添加到现有的类,并使用上下文来检索资产。在您的活动中调用如下方法:

public class MyActivity extends Activity {
  public void OnCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    NonActivity n = new NonActivity();
    n.doStuff(this);
  }
}
public class NonActivity {
    public void doStuff(Context c) {
        //read from assets
        c.getAssets();
        //use assets however
    }
}

Not sure what you're asking, but perhaps something like this? Just add to the existing class, and use the context to retrieve the assets. In your activity call the method like this:

public class MyActivity extends Activity {
  public void OnCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    NonActivity n = new NonActivity();
    n.doStuff(this);
  }
}
孤独难免 2024-10-08 08:46:35

要读取资源,您需要一个 Context,但您不必必须使用Activity作为您的Context;您可以使用 Application 对象代替。

Android 上下文不处于活动中?以及其他无活动编程?

public class MyApplication extends Application {
    private static MyApplication instance;

    public MyApplication() {
        instance = this;
    }

    public static MyApplication getInstance() {
         return instance;
    }
}

您需要将 android:name 属性添加到 AndroidManifest.xml 中的 元素首先:

 <application android:name="com.example.MyApplication" ... />

现在您可以从任何地方静态调用 MyApplication.getInstance().getAssets()

或者,您可以使用 Dagger 依赖项注入将 Application 直接注入到你的对象。 (注入 Application 上下文有点棘手。请参阅 Dagger 2 注入 Android上下文在 Danger github 存储库上提交的此问题。)

To read assets, you need a Context, but you don't have to use an Activity as your Context; you can use the Application object instead.

Android Context without being in an activity? And other activity-less programming?

public class MyApplication extends Application {
    private static MyApplication instance;

    public MyApplication() {
        instance = this;
    }

    public static MyApplication getInstance() {
         return instance;
    }
}

You'll need to add the android:name attribute to the <application> element in AndroidManifest.xml first:

 <application android:name="com.example.MyApplication" ... />

Now you can call MyApplication.getInstance().getAssets() statically from anywhere.

Alternately, you could use Dagger dependency injection to inject an Application directly into your object. (Injecting the Application context is a bit tricky. See Dagger 2 injecting Android Context and this issue filed on the Danger github repo.)

丘比特射中我 2024-10-08 08:46:35

请注意,所有文件系统访问都应在主线程之外完成,因此您不应在 onCreate() 期间读取它们。相反,您应该使用另一个线程,例如 AysncTask 提供的线程。

Note all file system accesses should be done off the main thread, so you shouldn't read them during onCreate(). Instead you should use another thread, such as provided by an AysncTask.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文