[英]Android getContext on a background Service
I'm trying to create a Service that runs even when my app is closed.我正在尝试创建一个即使在我的应用程序关闭时也能运行的服务。 However, I need to use my app Context inside this Service .
但是,我需要在此Service 中使用我的应用程序上下文。 When the app is running, the service works as well, but when I close the app (onDestroy() was called), the
getContext()
always returns null
.当应用程序运行时,服务也能正常工作,但是当我关闭应用程序时(调用了 onDestroy()),
getContext()
总是返回null
。
Service服务
public class SubscribeService extends Service {
private Context context;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
context = this; //Returns null when service is running on background
context = MyApp.getContext(); //Also null
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//do stuff using context
}
MyApp我的应用程序
public class MyApp extends Application {
private static Context context;
public static Context getContext() {
return context.getApplicationContext();
}
@Override
public void onCreate() {
context = getApplicationContext();
super.onCreate();
}
}
Service start from Activity onCreate()服务从 Activity onCreate() 开始
startService(new Intent(this, SubscribeService.class));
How should I use the Context in this scenario?在这种情况下我应该如何使用上下文?
Edit编辑
Managed to get it to work properly after Onik 's help.在Onik的帮助下设法让它正常工作。 I just had to call the
MyApp.getContext();
我只需要调用
MyApp.getContext();
before super.onCreate();
之前
super.onCreate();
Like so:像这样:
@Override
public void onCreate() {
context = MyApp.getContext();
super.onCreate();
}
Service extendsContext . 服务扩展了Context 。 You can use
this
, where this
is the reference to the Service
instance.您可以使用
this
, this
是对Service
实例的引用。
Putting more details on my comment below regarding the following code of SubscribeService class:在下面关于SubscribeService类的以下代码的评论中添加更多详细信息:
@Override
public void onCreate() {
super.onCreate();
context = this;
context = MyApp.getContext();
}
In your Service
's onCreate()
context = this
cannot be null
by a fundamental programming paradigm.在您的
Service
的onCreate()
context = this
,基本编程范式不能为null
。
Try this:尝试这个:
Added super.onCreate();
添加了
super.onCreate();
before MyApp.context = getApplicationContext();
之前
MyApp.context = getApplicationContext();
public class MyApp extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
MyApp.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApp.context;
}
}
Edit: Calling MyApp.getAppContext()
will return the application Context
.编辑:调用
MyApp.getAppContext()
将返回应用程序Context
。
already once left an answer , which was to use getApplicationContext()
in the Service
.已经有一次留下了答案,那就是在
Service
使用getApplicationContext()
。
also, using an IntentService with Context.startService(Intent)
might make sense here.此外,在这里使用带有
Context.startService(Intent)
的IntentService可能是有意义的。
... and do not insert statements before calling to super.onCreate()
. ...并且在调用
super.onCreate()
之前不要插入语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.