android隐藏前台服务通知,START_STICKY,前台Android服务在没有通知的情况下消失

我在我的新应用程序中启动了一项服务.该服务是有前途的,带有通知.当在AVD 2.1 API Level 7中运行时,一切正常.但是当它在运行Gingerbread的三星Galaxy Tab上运行时,服务将启动(图标和应用程序名称显示在通知区域的顶部),但几秒钟后,服务就会消失.我可以看到的Log中的最后一个条目与我的App相关联,是我的Log.d(“Taglines”,“Return with 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,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,"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的相关部分:

android:exported="false">

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值