
@OverrIDepublic 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 serviceD/Taglines(21863): TagsManager(nullary) completedD/Taglines(21863): onStartCommand()D/Taglines(21863): Calling super.onStartCommand()D/Taglines(21863): super.onStartCommand eturn code was 2D/Taglines(21863): createNotification calledD/Taglines(21863): Obtained reference to Notification ManagerD/Taglines(21863): createNotificacion() .. getApplicationContextD/Taglines(21863): createNotificacion() ... passing notificationD/Taglines(21863): Starting foregroundD/Taglines(21863): StartedD/Taglines(21863): Returning with 1
在那之后,没有什么特别的(PID 21863什么都没有).只是一堆:
D/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseW/inputManagerService( 302): Window already focused,ignoring focus gain of: com.androID.internal.vIEw.IinputMethodClIEnt$Stub$Proxy@40bc06e8D/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden falseD/KeyguardVIEwMediator( 302): setHIDden false
我不认为在这种情况下需要它,但这是AndroIDManifest.xml的相关部分:
<service androID:name=".TagsManager" androID:exported="false"> </service>
我哪里可能出错了?我可以提供哪些其他信息?
解决方法 一些东西:>摆脱mnotificationmanager.notify(NOTIFICATION_ID,通知);. startForeground()为您显示通知图标.>前景服务仍然可能被杀死,他们只是不太可能被杀死.> 2.3中存在一个错误(不确定它是否已修复),当服务被杀死并重新启动时,其onStartCommand()将不再被调用.相反,你将不得不在onCreate()中进行任何设置.
总结以上是内存溢出为你收集整理的START_STICKY,前台Android服务在没有通知的情况下消失全部内容,希望文章能够帮你解决START_STICKY,前台Android服务在没有通知的情况下消失所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)