窗口小部件启动服务也将启动主要活动 [英] Widget starting a service also starts main activity
问题描述
我有一个应该启动和停止服务(启动它时,它没有运行,停止它时,它是)一个小部件。这是工作的罚款,但是,每个服务启动时,我的应用程序的主要活动也推出了,这是我不想要的。
如果我删除主意图过滤器(<作用机器人:名字=android.intent.action.MAIN/>结果
),它,因为我希望它(作品,而无需启动主活动,只需服务),但我显然不具备主要活动了......
<类机器人:名字=android.intent.category.LAUNCHER/>从应用程序的清单
I have a widget that is supposed to start and stop a service (start it when it's not running, stop it when it is). This is working fine, however, each time the service is started, my app's main activity is also launched, which I don't want.
If I remove the MAIN-intent-filter (
<action android:name="android.intent.action.MAIN" />
) from the app's manifest, it works as I want it to (without launching the main-activity, just the service), but then I obviously don't have a main activity anymore...
<category android:name="android.intent.category.LAUNCHER" />
这是我如何启动服务(我假设这是正常的方式,我看不到任何参考什么可能导致的主要意图火):
This is how I start the service (I would assume this is the normal way, and I can't see any reference to what might cause the MAIN intent to fire):
Intent svc = new Intent(this, OnOffService.class);
startService(svc);
谢谢,结果
尼克
Thanks,
Nick
推荐答案
您的解决方案是不是一个好一个,例如在onPause()正好发生在消失当这种情况发生。
Your solution is not a good one, for example onPause() occurs when the user changes orientation (rotates the cell phone), so your app is going to just disappear when that happens.
我问,并找到了解决办法:<一href=\"http://stackoverflow.com/questions/4535673/why-does-starting-an-activity-from-a-widget-cause-my-main-activity-to-start-as-we/4540799#4540799\">Why并从部件开始的活动引起我的主要活动启动呢?
I asked and found the solution: Why does starting an activity from a widget cause my main activity to start as well?
这篇关于窗口小部件启动服务也将启动主要活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!