gpt4 book ai didi

android - START_STICKY,前台 Android 服务消失,恕不另行通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:40 24 4
gpt4 key购买 nike

我已经在我的新应用程序中启动了一项服务。该服务是前台的,带有通知。当它在 AVD 2.1 API Level 7 中运行时,一切正常。但是当它在运行 Gingerbread 的 Samsung Galaxy Tab 上运行时,该服务会启动(图标和应用程序名称出现在通知区域的顶部),但几秒钟后,该服务就会消失。我可以看到的日志中的最后一个条目与我的应用程序相关联,是我的 Log.d("Taglines","Returning with "+ START_STICKY) 的结果,它紧接在 "return START_STICKY ;"之前在我的服务的 onStartCommand 覆盖中,如下所示:

@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 ;
}

通知设置如下:

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");
}

这是启动服务时“adb logcat”的结果:

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 开始就没有了)。只是一堆:

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 的相关部分:

    <service android:name=".TagsManager"
android:exported="false">
</service>

我可能哪里出错了?我还可以提供哪些其他信息?

最佳答案

一些事情:

  1. 摆脱 mNotificationManager.notify(NOTIFICATION_ID, notification);startForeground() 为您显示通知图标。

  2. 前台 Service 仍然可以被终止,只是可能性较小。

  3. 2.3 中有一个错误(不确定是否已修复),当 Service 被终止并重新启动时,它的 onStartCommand() 将不会又叫了。相反,您必须在 onCreate() 中进行任何设置。

关于android - START_STICKY,前台 Android 服务消失,恕不另行通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6619143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com