Muh*_*ama 72 android foreground android-service
我想创建一个服务并让它在前台运行.
大多数示例代码都有通知.但我不想透露任何通知.那可能吗?
你能举个例子吗?还有其他选择吗?
我的应用服务正在做媒体播放器.如何使系统不杀死我的服务,除了应用程序自杀(如按钮暂停或停止音乐).
Kri*_*ski 84
作为Android平台的安全功能,在任何情况下,您都不能在没有通知的情况下拥有前瞻性服务.这是因为前景服务消耗了更多的资源并且受到与后台服务不同的调度限制(即,它不会被快速杀死),并且用户需要知道什么可能正在吃他们的电池.所以,不要这样做.
但是,它是可能有一个"假"的通知,也就是说,你可以做一个透明的通知图标(IIRC).这对您的用户来说是非常不诚实的,除了杀死他们的电池并因此产生恶意软件之外,您没有理由这样做.
Lio*_*luz 76
更新:这在Android 7.1上"已修复". https://code.google.com/p/android/issues/detail?id=213309
自4.3更新以来,基本上不可能在startForeground()
没有显示通知的情况下启动服务.
但是,您可以使用官方API隐藏图标...不需要透明图标:( NotificationCompat
用于支持旧版本)
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setPriority(Notification.PRIORITY_MIN);
Run Code Online (Sandbox Code Playgroud)
我已经和通知本身仍然需要存在的事实和平,但对于谁仍然想要隐藏它,我可能也找到了解决方法:
startForeground()
通过通知和所有内容启动虚假服务.startForeground()
(相同的通知ID)stopSelf()
onDestroy调用stopForeground(true)
).瞧!根本没有通知,您的第二个服务一直在运行.
Sam*_*Sam 19
这在Android 7.1+中不再有效.此技术可能违反Google Play的开发者政策(__CODE__
).
相反,我建议让用户阻止服务通知.
public class ForegroundService extends Service {
static ForegroundService instance;
@Override
public void onCreate() {
super.onCreate();
instance = this;
if (startService(new Intent(this, ForegroundEnablingService.class)) == null)
throw new RuntimeException("Couldn't find " + ForegroundEnablingService.class.getSimpleName());
}
@Override
public void onDestroy() {
super.onDestroy();
instance = null;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
public class ForegroundEnablingService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (ForegroundService.instance == null)
throw new RuntimeException(ForegroundService.class.getSimpleName() + " not running");
//Set both services to foreground using the same notification id, resulting in just one notification
startForeground(ForegroundService.instance);
startForeground(this);
//Cancel this service's notification, resulting in zero notifications
stopForeground(true);
//Stop this service so we don't waste RAM.
//Must only be called *after* doing the work or the notification won't be hidden.
stopSelf();
return START_NOT_STICKY;
}
private static final int NOTIFICATION_ID = 10;
private static void startForeground(Service service) {
Notification notification = new Notification.Builder(service).getNotification();
service.startForeground(NOTIFICATION_ID, notification);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
<service android:name=".ForegroundEnablingService" />
<service android:name=".ForegroundService" />
Run Code Online (Sandbox Code Playgroud)
经过测试和处理:
不再适用于Android 7.1.
Sni*_*las 15
你可以使用它(由@Kristopher Micinski建议):
Notification note = new Notification( 0, null, System.currentTimeMillis() );
note.flags |= Notification.FLAG_NO_CLEAR;
startForeground( 42, note );
Run Code Online (Sandbox Code Playgroud)
更新:
请注意,Android KitKat +版本不再允许这样做.请记住,这或多或少违反了Android中的设计原则,使@Kristopher Micinski提到的用户可以看到后台操作
phn*_*mnn 12
您可以使用 layout_height = "0dp" 的自定义布局在Android 9+上隐藏通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationUtils.CHANNEL_ID);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.custom_notif);
builder.setContent(remoteViews);
builder.setPriority(NotificationCompat.PRIORITY_LOW);
builder.setVisibility(Notification.VISIBILITY_SECRET);
Run Code Online (Sandbox Code Playgroud)
custom_notif.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在 Pixel 1、android 9 上测试。此解决方案不适用于 Android 8 或更低版本
Ang*_*i H 10
只需将通知的ID设置为零:
// field for notification ID
private static final int NOTIF_ID = 0;
...
startForeground(NOTIF_ID, mBuilder.build());
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotificationManager.cancel(NOTIF_ID);
...
Run Code Online (Sandbox Code Playgroud)
您可以获得的好处是Service
,除非在高内存压力下,否则将能够在不被Android系统破坏的情况下以高优先级运行.
编辑
要使其与Pre-Honeycomb和Android 4.4及更高版本一起使用,请确保使用NotificationCompat.Builder
Support Library v7提供的内容而不是Notification.Builder
.
这里的大多数答案要么不起作用,要么破坏前台服务,要么违反Google Play 政策。
可靠且安全地隐藏通知的唯一方法是让用户阻止它。
唯一的方法是阻止来自您的应用的所有通知:
将用户发送到应用程序的详细信息屏幕:
Uri uri = Uri.fromParts("package", getPackageName(), null);
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).setData(uri);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)让用户阻止应用程序的通知
请注意,这也会阻止您的应用程序的吐司。
在 Android O 上阻止通知是不值得的,因为操作系统只会将其替换为“在后台运行”或“使用电池”通知。
使用通知渠道来阻止服务通知而不影响您的其他通知。
将用户发送到通知频道的设置
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName())
.putExtra(Settings.EXTRA_CHANNEL_ID, myNotificationChannel.getId());
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)让用户屏蔽频道的通知
有一个解决方法.尝试在不设置图标的情况下创建通知,并且不会显示通知.不知道它是如何工作的,但它确实:)
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Title")
.setTicker("Title")
.setContentText("App running")
//.setSmallIcon(R.drawable.picture)
.build();
startForeground(101, notification);
Run Code Online (Sandbox Code Playgroud)
我将icon参数设置为Notification的构造函数为零,然后将生成的通知传递给startForeground().日志中没有错误,也没有显示任何通知.不过,我不知道这项服务是否成功有效 - 有什么方法可以检查吗?
编辑:使用dumpsys进行检查,确实该服务是在我的2.3系统上进行的.还没有检查过其他操作系统版本.
归档时间: |
|
查看次数: |
77492 次 |
最近记录: |