Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android O-单行通知-如;安卓系统-USB充电此设备“;_Android_Android Notifications_Android 8.0 Oreo - Fatal编程技术网

Android O-单行通知-如;安卓系统-USB充电此设备“;

Android O-单行通知-如;安卓系统-USB充电此设备“;,android,android-notifications,android-8.0-oreo,Android,Android Notifications,Android 8.0 Oreo,我想为我的ForegroundService提供一个持续的通知,它需要尽可能小的位置。我喜欢“安卓系统-USB充电这个设备”的风格,但我找不到任何例子如何实现这一点。 谁能给我指出正确的方向吗 更新 如果频道被分配了重要性重要性\u MIN,则通知将使用该样式 看起来没有办法使用Androids内置样式来通知重要性_MIN与ForegroundsService一起使用 以下是重要性\u MIN的说明: 最小通知重要性:仅显示在阴影处,折叠下方。这不应与Service.startForegrou

我想为我的
ForegroundService
提供一个持续的通知,它需要尽可能小的位置。我喜欢“安卓系统-USB充电这个设备”的风格,但我找不到任何例子如何实现这一点。

谁能给我指出正确的方向吗

更新 如果频道被分配了重要性
重要性\u MIN
,则通知将使用该样式

看起来没有办法使用Androids内置样式来通知
重要性_MIN
ForegroundsService一起使用

以下是
重要性\u MIN
的说明:

最小通知重要性:仅显示在阴影处,折叠下方。这不应与Service.startForeground一起使用,因为前台服务应该是用户关心的东西,因此将其通知标记为最低重要性在语义上没有意义。如果您在Android版本Build.version_code.O中执行此操作,系统将显示关于您的应用程序在后台运行的更高优先级通知


您需要将通知优先级设置为Min,将通知通道重要性设置为Min,并禁用显示通知通道标记

这是我如何做的一个例子。我还创建了完整的通知以供参考

private static final int MYAPP_NOTIFICATION_ID= -793531;

NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);

String CHANNEL_ID = "myapp_ongoing";
CharSequence name = context.getString(R.string.channel_name_ongoing);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_MIN);
    channel.setShowBadge(false);

    notificationManager.createNotificationChannel(channel);
}

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_stat_notification_add_reminder)
        .setContentTitle(context.getString(R.string.app_name))
        .setContentText(context.getString(R.string.create_new))
        .setOngoing(true).setWhen(0)
        .setChannelId(CHANNEL_ID)
        .setPriority(NotificationCompat.PRIORITY_MIN);

// Creates an intent for clicking on notification
Intent resultIntent = new Intent(context, MyActivity.class);
...

// The stack builder object will contain an artificial back stack
// for the
// started Activity.
// This ensures that navigating backward from the Activity leads out
// of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MyActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
        PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);

notificationManager.notify(MYAPP_NOTIFICATION_ID, mBuilder.build());

要显示紧凑的单线通知(如收费通知),您必须创建一个优先级为“重要性”的
通知频道

@TargetApi(Build.VERSION_CODES.O)
private static void createFgServiceChannel(Context context) {
   NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_MIN);
   NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
   mNotificationManager.createNotificationChannel(channel);
}
然后创建一个持续通知,如下所示:

public static Notification getServiceNotification(Context context) {
   NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, "channel_id");
   mBuilder.setContentTitle("One line text");
   mBuilder.setSmallIcon(R.drawable.ic_notification);
   mBuilder.setProgress(0, 0, true);
   mBuilder.setOngoing(true);
   return mBuilder.build();
}
注意

请注意,我已经用
IntentService
而不是
服务
对它进行了测试,它可以正常工作。另外,我刚刚检查了将
Thread.sleep()
设置为15秒,通知将完美显示,直到
IntentService
自动停止

有一些图片(抱歉,有些文字是西班牙语的,但我认为这些图片仍然有用):

如果向下拖动并打开通知,将显示如下:

额外的

如果您注意到Android系统显示一个通知,指示所有正在使用电池的应用程序(具有持续服务的应用程序),您可以降低此类通知的优先级,它将显示为单线通知,如充电通知

看看这个:

只需长按此通知,然后选择所有类别:

并将重要性设置为低:


下次,此“电池消耗”通知将显示为充电通知。

回答原始问题:

Android O上似乎没有内置的方式来获取一行持续的
ForegroundService
通知。可以尝试添加自定义设计,但由于不同的手机具有不同的通知设计,该解决方案很难成为一个好的解决方案

然而,还是有希望的:)


Android p上,优先级为
priority\u LOW
NotificationChannel
中的
重要性
通知被压缩到一行,即使对于
ForegroundService
。是的

通过创建一个空的自定义视图,我减小了前台服务通知的大小,如下所示:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


</LinearLayout>
RemoteViews notifiactionCollapsed = new RemoteViews(getPackageName(),R.layout.notification_collapsed);
    Notification notification = new NotificationCompat.Builder(this,CHANNEL_ID)
            .setSmallIcon(R.drawable.eq_icon)
            .setCustomContentView(notifiactionCollapsed)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setShowWhen(false)
            .setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setOngoing(true)
            .setVisibility(NotificationCompat.VISIBILITY_SECRET)
            .build();
    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
            notification);

这有助于降低通知的高度,但我仍然不确定如何隐藏通知图标。

谢谢@CodeChimp的回答。我试着把优先级设置得很低。一方面,它不起作用。通知的样式没有更改为单行。另一方面,Android服务的文档明确指出,您需要将优先级设置为最低
priority\u LOW
[。因此,当优先级设置为priority\u MINI时,我的服务很快就被终止了。我没有将前台服务用于我的服务,它只在单击时触发活动。channel.setShowBadge(错误);也没有帮助?我已经在使用
setShowBadge(错误)
了,不幸的是,它也不起作用。我还在试验,但很难相信还没有这样的例子:)恐怕这是我回答的极限。离开这里,以防有人不需要前台服务通知,但有兴趣看到答案。@Szobi您使用的是
IntentService
还是
service
?我使用的是前台
IntentService
,我的一行无声通知效果很好墨迹技巧应该是重要的最小设置。下面是它的说明:"最小通知重要性:仅显示在折叠下方的阴影处。这不应与Service.startForeground一起使用,因为前台服务应该是用户关心的东西,因此将其通知标记为最小重要性在语义上没有意义。如果您在Android版本
Build.version\u CODES.O中这样做ode>,系统将显示有关在后台运行的应用程序的更高优先级通知。"这意味着,Foregroundservices可能不可能这就是为什么我写了额外的提示,为消耗电池的应用程序设置低优先级的系统通知,但不幸的是,这必须由用户完成,而且大部分可能是通过编程来完成的。在我的情况下,这是有效的,因为我做了这个技巧,并且我使用了一个非常简单的fast
IntentService
。如果这不是您期望的答案,很抱歉。我会给您奖金,因为这个答案让我意识到(并实际阅读了正确的文档)抱歉@CodeChimp,事实上你的答案已经很接近了,但是Marc更需要这个名声:)哇,谢谢@Szobi,很惊讶,因为这是我的第一个博客