Android API>25中前台服务的应用程序运行(可见)时是否必须发出通知
从stackoverflow和许多博客中,我确实了解到,在API>25中,前台服务在没有通知的情况下是不会运行的。但我仍然混淆了当应用程序在屏幕上运行或可见时,这是通知强制。 例如,当用户站在应用程序内时,不需要通知。那么,这是否可以在应用程序运行时删除通知? 在职班Android API>25中前台服务的应用程序运行(可见)时是否必须发出通知,android,android-8.0-oreo,foreground-service,Android,Android 8.0 Oreo,Foreground Service,从stackoverflow和许多博客中,我确实了解到,在API>25中,前台服务在没有通知的情况下是不会运行的。但我仍然混淆了当应用程序在屏幕上运行或可见时,这是通知强制。 例如,当用户站在应用程序内时,不需要通知。那么,这是否可以在应用程序运行时删除通知? 在职班 @Override public int onStartCommand(Intent intent, int flags, int startId) { ...... if (Build.VERSION.SDK_I
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
......
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder builder = new Notification.Builder(this, ANDROID_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText(text)
.setAutoCancel(true);
Notification notification = builder.build();
startForeground(1, notification);
}
return START_NOT_STICKY;
}
活跃
Intent myService = new Intent(this, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(myService);
} else {
startService(myService);
}
不,这是强制性的,即使你的应用程序正在前台运行。你的前台服务需要通知 你无法隐藏它 原因: 您可以使用任何其他后台任务处理程序,如intent service,工作sclr但事情的设计不同于前台服务您的用户了解事件我将关闭此事件其中一个进程将继续运行,但事情与后台服务不同您知道它将在后台执行某些操作,但当系统决定执行此操作的最佳时间时,而不是当您的应用程序希望在前台执行类似操作时服务 还有一个例子: 假设您的应用程序在前台的电池电量低于用户或系统的预期,则无论发生什么情况,您的前台服务都将立即执行,因此,您的用户必须知道它正在运行,并获取我的资源电池、数据等
希望你明白我的意思不,这是强制性的,即使你的应用程序正在前台运行,你的前台服务也需要通知 你无法隐藏它 原因: 您可以使用任何其他后台任务处理程序,如intent service,工作sclr但事情的设计不同于前台服务您的用户了解事件我将关闭此事件其中一个进程将继续运行,但事情与后台服务不同您知道它将在后台执行某些操作,但当系统决定执行此操作的最佳时间时,而不是当您的应用程序希望在前台执行类似操作时服务 还有一个例子: 假设您的应用程序在前台的电池电量低于用户或系统的预期,则无论发生什么情况,您的前台服务都将立即执行,因此,您的用户必须知道它正在运行,并获取我的资源电池、数据等
希望您明白我的意思在前台服务运行时不可能删除通知,但是可以将前台服务更改回常规服务。这样就不需要通知了。实际上,要使用的函数 …包括仅用于此目的的removeNotification参数。通过交替调用startForeground和stopForeground,您可以根据需要将服务从前台切换为常规服务 如果不清楚,您可能希望在至少有一个活动处于启动状态时调用stopForeground。这是您必须手动跟踪的内容。然后,当启动的活动数达到0时,您将调用startForeground 编辑 一种方法是使用绑定服务。然后,当您需要时,可以很容易地在其上调用stopForeground 假设您有一项活动。您可以将其绑定到服务see或use。然后,您的onServiceConnected函数可能看起来像是根据Google示例改编的:
//MyActivity.java:
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mService.stopForeground(true); //This makes the notification go away
bound = true;
}
...
@Override
protected void onStart() {
super.onStart();
// Bind to the service
bindService(new Intent(this, MyService.class), this, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (bound) {
Notification.Builder builder = new Notification.Builder(this, ANDROID_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText(text)
.setAutoCancel(true);
Notification notification = builder.build();
mService.startForeground(1, notification); //This brings the notification back! Service is already running, and continues to run.
unbindService(this);
bound = false;
}
}
在前台服务运行时无法删除通知,但可以将前台服务更改回常规服务。这样就不需要通知了。实际上,要使用的函数 …包括仅用于此目的的removeNotification参数。通过交替调用startForeground和stopForeground,您可以根据需要将服务从前台切换为常规服务 如果不清楚,您可能希望在至少有一个活动处于启动状态时调用stopForeground。这是您必须手动跟踪的内容。然后,当启动的活动数达到0时,您将调用startForeground 编辑 一种方法是使用绑定服务。然后,当您需要时,可以很容易地在其上调用stopForeground 假设您有一项活动。您可以将其绑定到服务see或use。然后,您的onServiceConnected函数可能看起来像是根据Google示例改编的:
//MyActivity.java:
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mService.stopForeground(true); //This makes the notification go away
bound = true;
}
...
@Override
protected void onStart() {
super.onStart();
// Bind to the service
bindService(new Intent(this, MyService.class), this, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (bound) {
Notification.Builder builder = new Notification.Builder(this, ANDROID_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText(text)
.setAutoCancel(true);
Notification notification = builder.build();
mService.startForeground(1, notification); //This brings the notification back! Service is already running, and continues to run.
unbindService(this);
bound = false;
}
}
在API 26之前,我的整个应用程序在运行模式下由后台服务更新,但现在在25之后,我如何在没有前台服务的情况下每秒更新我的应用程序,因为有刺激性通知,请根据您的调整需求建议作业计划程序、意图服务或更多作业计划程序,每次或特定时间段都会更新意向服务?请尝试在任何前台或后台线程下以您想要的更新时间运行CountDownTimer。在API 26之前,后台服务在运行模式下更新我的整个应用程序,但现在在25之后,我如何在没有前台服务的情况下每秒钟更新我的应用程序,因为有刺激性通知,请根据您的调整需求,在中建议作业计划程序、意向服务或更多作业计划程序
帐篷服务每一次或某一段时间都会更新?请尝试在任何前台或后台线程下运行带有更新时间的倒计时程序。经过一段漫长的旅程后,得到了一些有趣的答案…请提供一些示例抱歉,我现在还不知道任何示例。我没听说过你经常想做什么。我的建议是在谷歌上搜索它,然后按照docs.前台服务返回常规服务。??如何做到这一点…从主要活动开始ForgRoundService之后经过长途旅行得到了一些有趣的答案…请提供一些示例抱歉,我现在还不知道任何示例。我没听说过你经常想做什么。我的建议是在谷歌上搜索它,然后按照docs.前台服务返回常规服务。??如何执行此操作…从主活动启动ForgRoundService后