START_STICKY,前台的Android服务消失,恕不另行通知 [英] START_STICKY, foreground Android service goes away without notice
问题描述
我已经开始在我的新的应用服务。该服务被前景化,以通知。当这是运行在AVD 2.1 API 7级,一切工作正常。但是,当它运行在三星Galaxy Tab运行姜饼,该服务将启动(图标和应用程序名称出现在通知区域的顶部),但几秒钟后,该服务就会消失。在我能看到的是我的应用程序相关的日志,最后一项是我Log.d的结果(口头禅,+ START_STICKY与回归),这立即precedes返回START_STICKY;在我服务的onStartCommand覆盖,如下所示:
I have started a service in my new application. The service is foregrounded, with a Notification. When this is run in the AVD 2.1 API Level 7, all works fine. But when it's run on a Samsung Galaxy Tab running Gingerbread, the service will start (the icon and app name appear at the top of the notification area), but after a few seconds, the service disappears. The last entry in the Log that I can see is associated with my App, is the result of my Log.d("Taglines","Returning with " + START_STICKY), which immediately precedes "return START_STICKY ;" in my Service's onStartCommand override, as follows:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int rc ;
Log.d("Taglines","onStartCommand()");
Toast.makeText(this, "Starting service TagsManager", Toast.LENGTH_SHORT).show();
Log.d("Taglines","Calling super.onStartCommand()");
rc = super.onStartCommand(intent,flags,startId);
Log.d("Taglines","super.onStartCommand return code was " + rc);
createNotification(INITIAL_NOTIFICATION_TEXT);
Log.d("Taglines","Returning with " + START_STICKY);
return START_STICKY ;
}
的通知设置是这样的:
The notification is set like this:
void createNotification(String text) {
Log.d("Taglines","createNotification called");
if (mNotificationManager == null) {
// Get a reference to the Notification Manager
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
Log.d("Taglines","Obtained reference to Notification Manager");
}
// Instantiate the Notification
int icon = R.drawable.ic_notification;
CharSequence tickerText = "Taglines";
long when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
// Define Notification's expanded message and intent
Log.d("Taglines","createNotificacion() .. getApplicationContext");
context = getApplicationContext();
contentText = text;
// notificationIntent = new Intent(this, TagsOverview.class);
notificationIntent = new Intent(this, TagsServiceMenu.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
// Pass the Notification to the NotificationManager:
Log.d("Taglines","createNotificacion() ... passing notification");
mNotificationManager.notify(NOTIFICATION_ID, notification);
Log.d("Taglines","Starting foreground");
startForeground(NOTIFICATION_ID, notification);
Log.d("Taglines","Started");
}
这是从亚行logcat时,启动该服务的结果:
This is the result from "adb logcat" when the service is started:
D/Taglines(21863): Starting service
D/Taglines(21863): TagsManager(nullary) completed
D/Taglines(21863): onStartCommand()
D/Taglines(21863): Calling super.onStartCommand()
D/Taglines(21863): super.onStartCommand eturn code was 2
D/Taglines(21863): createNotification called
D/Taglines(21863): Obtained reference to Notification Manager
D/Taglines(21863): createNotificacion() .. getApplicationContext
D/Taglines(21863): createNotificacion() ... passing notification
D/Taglines(21863): Starting foreground
D/Taglines(21863): Started
D/Taglines(21863): Returning with 1
在这之后,没有什么特别的(什么都没有从PID 21863)。只是一堆:
After that, nothing special (nothing at all from PID 21863). Just a bunch of:
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
W/InputManagerService( 302): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40bc06e8
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
D/KeyguardViewMediator( 302): setHidden false
我不认为它需要在这种情况下,但这里的AndroidManifest.xml中的相关部分:
I don't think it's needed in this case, but here's the relevant portion of the AndroidManifest.xml:
<service android:name=".TagsManager"
android:exported="false">
</service>
如果可能我已经错了吗?我可以提供其他什么信息?
Where might I have gone wrong? What other information can I provide?
推荐答案
有几件事情:
-
摆脱
mNotificationManager.notify(NOTIFICATION_ID,通知);
。startForeground()
为您显示的通知图标。
Get rid of the
mNotificationManager.notify(NOTIFICATION_ID, notification);
.startForeground()
displays the notification icon for you.
前景服务
S能仍然被杀死,他们只是不太可能。
Foreground Service
s can still be killed, they're just less likely to be.
有一个在2.3中的错误(不知道它是固定的还),其中当服务
将被注销,重启,其 onStartCommand ()
将不会被再次调用。相反,你打算做任何设置在的onCreate()
。
There's a bug in 2.3 (not sure if it was fixed yet) where when a Service
is killed and restarted, its onStartCommand()
will NOT be called again. Instead you're going to have to do any setting up in onCreate()
.
这篇关于START_STICKY,前台的Android服务消失,恕不另行通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!