Android Context没有参加活动?和其他无活动的编程? [英] Android Context without being in an activity? And other activity-less programming?
问题描述
我会尽力把这个问题变成一个全面的问题:
我正在编写一种获取String的方法,该字符串包含由LocationManager
和getLastKnownLocation()
以及所有其他内容确定的Android设备城市的名称.
然后我意识到我需要在另一个活动中再次做同样的事情,所以为什么不制作一个可以在整个程序中使用的完全独立的类(LocationFinder
),而不是在各处编写重复的代码? /p>
但是我遇到了使我感到困惑的问题.例如,如果我创建此类(LocationFinder
),即使从未真正可视化它,它也应该扩展Activity吗?该类所要做的就是拥有各种getLastKnownCity()
或getCurrentCity()
这样的getter并返回字符串.我以为不必扩展Activity类,因为它实际上不是活动.
但是接下来我要使用什么上下文:
Geocoder geocoder = new Geocoder(Context context, Locale locale)
?
这使我认为它必须是一项活动.所以我扩展了Activity,并用
替换了构造函数@Override
protected void onCreate(..............
但是由于某些原因,即使我放了,它也永远不会被呼叫
String city = new LocationFinder().getLastKnownCity();
LocationFinder
的onCreate()
的第一行是
System.out.println("HEY!")
,甚至都没有做到这一点.我在android.internal.os.LoggingPrintStream.println()
和其他地方得到了空指针.
此外,有许多来自Activity类的系统常量.例如,我需要获取LOCATION_SERVICE
,它是一个字符串,如果不扩展Activity则无法获取.当然,我可以作弊,只需输入文字字符串,但这感觉是错误的.
构造类时,可以有一个构造函数,该构造函数接受Context并将其分配给您的类中的本地Context对象.
public class LocationFinder {
private Context myContext;
private Geocoder geocoder;
public LocationFinder(Context context)
{
myContext = context;
geocoder = new Geocoder(myContext);
}
}
然后,当您尝试访问此类时,请确保将其初始化为:
public class TestActivity extends Activity {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationFinder lFinder = new LocationFinder(getApplication());
}
}
当然,您不能从将要运行的每个类中访问上下文.因此,对View的引用就足够了.
LocationFinder lFinder = new LocationFinder(anyView.getApplication());
I'll try really hard to turn this into one comprehensive question:
I'm writing a method to get a String that contains the name of an Android device's city, as determined by the LocationManager
and getLastKnownLocation()
and all that.
Then I realized I'd need to do the same thing again in another activity, so why not just make an entirely separate class (LocationFinder
) that I could use across my program, instead of writing duplicate code everywhere?
But I've run into problems that confuses me. For instance, if I make this class (LocationFinder
), should it extend Activity, even though it is never actually visualized? All this class would do is have a variety of getters like getLastKnownCity()
or getCurrentCity()
and return strings. I assumed it wouldn't HAVE to extend the Activity class, since it's really not an activity.
But then what Context do I use for:
Geocoder geocoder = new Geocoder(Context context, Locale locale)
?
This made me assume it MUST be an activity. So I extended Activity, and replaced the constructor with
@Override
protected void onCreate(..............
but for some reason, that never ends up getting called, even when I put
String city = new LocationFinder().getLastKnownCity();
My very first line of LocationFinder
's onCreate()
is
System.out.println("HEY!")
and it never even gets to that. I get a null pointer at android.internal.os.LoggingPrintStream.println()
and other stuff.
Plus, there's a bunch of system constants that come from Activity classes. For instance, I need to get at LOCATION_SERVICE
, which is a String, which I can't get without extending Activity. Sure, I could cheat and just put in the literal string, but that feels wrong.
When constructing your class, you can have a constructor that takes in a Context and assigns it a local Context object within your class.
public class LocationFinder {
private Context myContext;
private Geocoder geocoder;
public LocationFinder(Context context)
{
myContext = context;
geocoder = new Geocoder(myContext);
}
}
And then when you try to access this class, make sure you initialise it like:
public class TestActivity extends Activity {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationFinder lFinder = new LocationFinder(getApplication());
}
}
Of course, you can't access a context from every class that you will be running. So a reference to a View can suffice.
LocationFinder lFinder = new LocationFinder(anyView.getApplication());
这篇关于Android Context没有参加活动?和其他无活动的编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!