一个情境或活动的GetString的外 [英] getString Outside of a Context or Activity

查看:123
本文介绍了一个情境或活动的GetString的外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了 R.string pretty的真棒保持硬codeD串出我的code,我想保持与模特的工作在我的应用程序生成的输出一个工具类,使用它。例如,在这种情况下,我生成从一个模型的电子邮件活动的外面。

I've found the R.string pretty awesome for keeping hardcoded strings out of my code, and I'd like to keep using it in a utility class that works with models in my application to generate output. For instance, in this case I am generating an email from a model outside of the activity.

是否可以使用的getString A 上下文活动外 ?我想我可以通过在目前的活动,但似乎没有必要。请纠正我,如果我错了!

Is it possible to use getString outside a Context or Activity? I suppose I could pass in the current activity, but it seems unnecessary. Please correct me if I'm wrong!

编辑:我们可以访问资源的没有的使用上下文

Can we access the resources without using Context?

推荐答案

不幸的是,你可以访问任何字符串资源的唯一方法是使用一个上下文(即一个活动服务)。我在这种情况下所通常是,是简单地要求调用者传递的背景下。

Unfortunately, the only way you can access any of the string resources is with a Context (i.e. an Activity or Service). What I've usually done in this case, is to simply require the caller to pass in the context.

这篇关于一个情境或活动的GetString的外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆