- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经在我的新应用程序中启动了一项服务。该服务是前台的,带有通知。当它在 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>
我可能哪里出错了?我还可以提供哪些其他信息?
最佳答案
一些事情:
摆脱 mNotificationManager.notify(NOTIFICATION_ID, notification);
。 startForeground()
为您显示通知图标。
前台 Service
仍然可以被终止,只是可能性较小。
2.3 中有一个错误(不确定是否已修复),当 Service
被终止并重新启动时,它的 onStartCommand()
将不会又叫了。相反,您必须在 onCreate()
中进行任何设置。
关于android - START_STICKY,前台 Android 服务消失,恕不另行通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6619143/
我正在关注 Android guide在媒体播放器应用程序中使用 MediaBrowserServiceCompat,但该服务在应用程序退出时被销毁。
我使用System.Windows.Window.IsActive来检测窗口是否位于前台,并且在某些情况下有效。但我发现了一些情况,它没有,我想知道是否有任何方法可以检测到它。 最佳答案 除非仅后台进
我需要为 EditText 的一部分设置样式。我希望文本为白色,背景为灰色。看起来很简单,但事实并非如此。 spanRange.setSpan(new BackgroundColorSpan(Colo
我决定在一个帖子中发布两个问题,因为这是完全相同的问题。 我需要知道屏幕何时打开或关闭,以便我可以打开 LED。第二个我需要知道我的应用程序是在后台还是在前台,以便在应用程序处于后台时管理在某些操作上
关于双高红色录音状态栏有很多问题( here , here ),但是当应用程序退出到后台时,所有这些问题都引用闪烁。我得到了一个闪光,我假设来自 AVCaptureSession设置,而应用程序在前台
我有一个奇怪的案例。我的 swift ios 应用程序已连接到 Cloudkit。如果应用程序未运行(后台状态),我每次都会收到通知徽章和警报!如果应用程序正在运行,则不会收到任何通知!我知道它没有击
我是 firebase 和 android 的新手,我想在我的应用程序中包含实时聊天。但我对 firebase 有以下疑问。请帮忙。 1) 如果应用程序在前台,系统托盘中是否会有默认通知,还是我必须在
我正在为 Excel 编写 VSTO 加载项,我注意到,如果我锁定工作表并使用密码保护它(因此只有我的加载项可以写入它,但用户可以查看它),如果用户尝试编辑工作表时,他们收到“此工作表已锁定”弹出窗口
我正在开发一个 iOS 应用程序,此应用程序允许其用户添加他稍后必须执行的任务。完成添加此任务后,它将发送到服务器以保存在服务器端。现在我对某些情况感到困惑:我的用户在输入任务详细信息时有什么电话..
我正在阅读内存不足 (OOM) killer ,以及 Android 如何确定进程的优先级 (https://developer.android.com/guide/components/proces
我已经在我的新应用程序中启动了一项服务。该服务是前台的,带有通知。当它在 AVD 2.1 API Level 7 中运行时,一切正常。但是当它在运行 Gingerbread 的 Samsung Gal
我的 Laravel 应用程序的结构需要帮助。 我想要的基本上是这个结构: 应用程序接口(interface) 管理面板 公共(public)网站 我开始构建我认为非常正确的文件夹结构: app/
我正在尝试用 CardView 填充我的 RecyclerView,CardView 使用 Android 数据绑定(bind)来设置属性,例如 TextView 中的文本。在未完成喷射的项目上,我想
我想使用 Window Script Host(WSH) 查找当前事件(具有焦点)的窗口的标题,因为我希望我的 WSH 脚本仅在所需窗口处于事件状态时才发送键。 注意*我无法使用替代方案,即在调用 s
如何调试 react-scripts 启动? 这工作正常,我不知道发生了什么变化(我没有改变任何东西) 看来 react-scripts start 无法作为前台进程继续运行。 我的 Dockerfi
我想制作像endonmondo那样的秒表。当我们启动应用程序时,它应该计算时间并更新主 Activity 中的textView(实际上它是一个 fragment )。 我做了后台服务(如 here )
我遇到了一个需求,但我无法获得正确的实现方式,因此需要您的帮助。 我想做什么? - 我想根据收到的通知执行操作,如下所示: 当应用程序打开并位于前台时,即对用户可见并且我收到通知时,我只是显示一个弹出
在 Android 10 中,我注意到我从操作系统收到一条 Toast 消息,说明“此 NFC 标签不支持应用程序”或“此 NFC 标签不支持应用程序”(取决于设备): 奇怪的是,我在 enableR
我是一名优秀的程序员,十分优秀!