起价应用类服务 [英] Starting a service from application class
本文介绍了起价应用类服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
startService
我可以从应用程序#的onCreate()?
块引用>我们能否从应用程序类启动服务?
我想我的应用程序启动时启动的服务。因此,而不是从起点开始的活动服务的,我可以用我的应用程序类呢?谢谢你。
我试过是内部应用程序类:
公共无效的onCreate(){
super.onCreate();
Log.i(记录器,服务启动);
意图errorLoggerService =新意图(getApplicationContext()
ErrorLoggerService.class);
。getApplicationContext()startService(errorLoggerService);
}
解决方案是的,你可以这样做:
公共类MyApplication的扩展应用{
公共无效的onCreate(){
super.onCreate();
startService(新意图(这一点,MyService.class));
}
}修改
不要叫getApplicationContext(),只需调用startService()。此外,请确保你已经宣布该服务在您的清单中。Possible Duplicate:
Can I startService from Application#onCreate()?Can we start a service from the application class?
I want to start a service when my application starts. So instead of starting the service from the starting activity, can I use my application class for this? Thanks.
What I tried is Inside application class :
public void onCreate() { super.onCreate(); Log.i("Logger", "Service Starting"); Intent errorLoggerService = new Intent(getApplicationContext(), ErrorLoggerService.class); getApplicationContext().startService(errorLoggerService); }
解决方案Yes, you can do this:
public class MyApplication extends Application { public void onCreate() { super.onCreate(); startService(new Intent(this, MyService.class)); } }
EDIT Don't call getApplicationContext(), just call startService(). Also, make sure you've declared the service in your manifest.
这篇关于起价应用类服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文