我正在尝试编写一个应用程序,它会在经过一段时间后返回到前台时执行某些特定操作.有没有办法检测应用程序何时发送到后台或带到前台?
我Service
在Android O OS上使用Class.
我计划Service
在后台使用.
Android建议指出startService()
应该使用startForegroundService()
.
如果您使用startForegroundService()
,则Service
抛出a Service
然后调用Service
错误.
这有什么问题?
service android operating-system foreground android-8.0-oreo
我在Android中有一个前台服务设置.我想更新通知文本.我正在创建服务,如下所示.
如何更新在此前台服务中设置的通知文本?更新通知的最佳做法是什么?任何示例代码将不胜感激.
public class NotificationService extends Service {
private static final int ONGOING_NOTIFICATION = 1;
private Notification notification;
@Override
public void onCreate() {
super.onCreate();
this.notification = new Notification(R.drawable.statusbar, getText(R.string.app_name), System.currentTimeMillis());
Intent notificationIntent = new Intent(this, AbList.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
this.notification.setLatestEventInfo(this, getText(R.string.app_name), "Update This Text", pendingIntent);
startForeground(ONGOING_NOTIFICATION, this.notification);
}
Run Code Online (Sandbox Code Playgroud)
我正在我的主要活动中创建服务,如下所示:
// Start Notification Service
Intent serviceIntent = new Intent(this, NotificationService.class);
startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud) 更新:我还没有找到问题的真正解决方案.我所做的是一种在连接丢失时自动重新连接到以前的蓝牙设备的方法.它并不理想,但似乎运作得相当好.我很想听到有关这个的更多建议.
我遇到的问题与此问题大致相同:服务在被唤醒时被杀死并且在调用startForeground之后包括设备(Asus Transformer),服务停止前的时间长度(30-45分钟),使用唤醒锁定,使用startForeground(),以及当屏幕关闭时应用程序打开时不会发生问题的事实.
我的应用程序维护与另一台设备的蓝牙连接,并在两者之间发送数据,因此它必须始终处于活动状态以侦听数据.用户可以随意启动和停止服务,实际上这是我实现启动或停止服务的唯一方法.服务重新启动后,与其他设备的蓝牙连接将丢失.
根据链接问题中的答案,startForeground()"降低了服务被杀的可能性,但并未阻止它".我理解是这样的,但是我已经看到很多其他应用程序没有这个问题的例子(例如,Tasker).
如果服务能够在用户停止之前运行,我的应用程序的实用性将大大降低.有什么办法可以避免这个???
每当服务停止时,我都会在logcat中看到这个:
ActivityManager: No longer want com.howettl.textab (pid 32321): hidden #16
WindowManager: WIN DEATH: Window{40e2d968 com.howettl.textab/com.howettl.textab.TexTab paused=false
ActivityManager: Scheduling restart of crashed service com.howettl.textab/.TexTabService in 5000ms
Run Code Online (Sandbox Code Playgroud)
编辑:我还应该注意,这似乎没有出现在我连接的其他设备上:HTC Legend运行Cyanogen
编辑:这是输出adb shell dumpsys activity services
:
* ServiceRecord{40f632e8 com.howettl.textab/.TexTabService}
intent={cmp=com.howettl.textab/.TexTabService}
packageName=com.howettl.textab
processName=com.howettl.textab
baseDir=/data/app/com.howettl.textab-1.apk
resDir=/data/app/com.howettl.textab-1.apk
dataDir=/data/data/com.howettl.textab
app=ProcessRecord{40bb0098 2995:com.howettl.textab/10104}
isForeground=true foregroundId=2 foregroundNoti=Notification(contentView=com.howettl.textab/0x1090087 vibrate=null,sound=null,defaults=0x0,flags=0x6a)
createTime=-25m42s123ms lastActivity=-25m42s27ms
executingStart=-25m42s27ms restartTime=-25m42s124ms
startRequested=true stopIfKilled=false callStart=true lastStartId=1
Bindings:
* IntentBindRecord{40a02618}:
intent={cmp=com.howettl.textab/.TexTabService}
binder=android.os.BinderProxy@40a9ff70
requested=true received=true hasBound=true doRebind=false
* Client AppBindRecord{40a3b780 ProcessRecord{40bb0098 …
Run Code Online (Sandbox Code Playgroud) 我想创建一个服务并让它在前台运行.
大多数示例代码都有通知.但我不想透露任何通知.那可能吗?
你能举个例子吗?还有其他选择吗?
我的应用服务正在做媒体播放器.如何使系统不杀死我的服务,除了应用程序自杀(如按钮暂停或停止音乐).
我想让我Service
在前台跑步.我尝试使用此示例(请查看" 在前台运行服务 "部分),但startForeground()
实际上并未显示我的通知.并没有抛出异常.为了使它显示,我需要使用NotificationManager
像这里解释的那样.随着NotificationManager
我的通知工作,但我不确定我Service
的前景是在这个"无声"呼叫之后startForeground()
.
有什么不对?
编辑:我刚刚测试了这个应该演示的示例项目startForeground()
,但它不起作用!我使用API v7.0,我在模拟器和真实设备(SE Xperia Neo)上测试了它.通知不会出现.
EDIT2:如果我试着打电话,setForeground()
那我就收到了警告setForeground: ignoring old API call
.
我也尝试使用这里startForegroundCompat()
描述的,但效果绝对相同.我按照这里的描述检查我的服务是否是前台服务,我发现我的服务不是前台服务.ActivityManager.RunningServiceInfo
我想为网页上的元素添加一些亮点.如果我不必在页面中添加额外的html,我更愿意.我希望图像出现在元素的前面而不是后面.最好的方法是什么?
在Windows中,前台窗口和活动窗口有什么区别?具体而言,前景窗口在什么情况下不能成为活动窗口?如果2个术语指的是相同的概念,为什么有2个术语.
这里的msdn文档提到"单击一个窗口,或者使用ALT+ TAB或ALT+ ESC组合键"使窗口处于活动状态和前景.没有明确说明2个术语之间的区别.检查MSDN.
我想找到一种方法来查看在前台运行的应用程序,或者当我的应用程序显示本地通知时是否显示主屏幕.例如,如果有主屏幕或其他应用程序,我想要有不同的操作.我尝试使用已处理和pid,但是应用程序启动时会生成pid,而不是最后一次使用应用程序.任何的想法?谢谢
我正在开发一个音乐/视频播放器应用程序,只需要知道如何在我的应用程序处于前台时禁用自动锁定.
我知道我已经使用[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
,并[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
在某些时候,但如果是把他们最好的地方?
foreground ×10
android ×6
service ×4
ios ×2
iphone ×2
auto-lock ×1
background ×1
css ×1
css3 ×1
html ×1
ipad ×1
multitasking ×1
winapi ×1
window ×1