我在自己的进程中有一个服务,我在 list 中声明了它:
<service
android:name="com.discountscatcher.util.PointService"
android:configChanges="orientation"
android:process=":pointservice" >
</service>
但是 onStartCommand
我试图获取一个 ApplicationContext
,但它总是返回 null,我能忘记什么吗?
public int onStartCommand(Intent intent, int flags, int startId) {
android.os.Debug.waitForDebugger();
Context mContext = getApplicationContext();
return Service.START_STICKY;
}
然后我就这样开始了:
startService(new Intent(getApplicationContext(), PointService.class));
在 Myactivity OnCreate
中
有什么想法吗?
最佳答案
您正在不同的进程中运行您的服务
,因此它与实际应用程序进程没有通信。
尝试从您的服务中删除这一行
android:process=":pointservice"
应用程序完成后,应用程序上下文变为空。在大多数情况下,不需要在不同的进程中运行该服务。也不推荐。
更多信息请点击此处
关于android - getApplicationContext() 在服务中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20676233/