我有一个服务,在其onCreate()方法中,我尝试获取LocationManager类的实例。
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
在我的活动中,我创建了一个新意图并启动了该服务。
Intent t = new Intent(MainActivity.this, MyService.class);
MainActivity.this.startService(t);
只要我以这种方式在清单中注册服务,它就能正常工作:
<service
android:name="de.path.MyService" />
关键是我想在自己的进程中运行服务,所以我添加到服务声明中:
android:process=":MyGPSProcess"
现在启动服务我得到一个NullPointerException,告诉我LocationManager中的ILocationManager mService为空。
是否没有可用的上下文?有没有办法获得SystemService?