棒棒糖通知setVisibility()不工作?
我想写使用的演示 setVisibility()
来控制显示的内容在Android 5.0锁屏的通知
。然而,似乎没有任何影响:
I am trying to write a demo of using setVisibility()
to control what shows up on the Android 5.0 lockscreen for a Notification
. However, there seems to be no effect:
-
默认
VISIBILITY_PRIVATE
仍显示私营通知
,而不是它的公共对口
the default
VISIBILITY_PRIVATE
still shows the privateNotification
, not its public counterpart
VISIBILITY_SECRET
的通知仍然在锁屏显示
VISIBILITY_SECRET
notifications still show up on the lockscreen
IOW,一切行为就像 VISIBILITY_PUBLIC
已生效,至少在我测试的一台Nexus 7上运行,我们不久均给予了Android 5.0图像后的Android 5.0发布(建LPX13D)。所以,我不知道这个问题是依赖于我的code,该设备,或者在错误的Android。
IOW, everything behaves as though VISIBILITY_PUBLIC
were in force, at least when I test on a Nexus 7 running the Android 5.0 image we were given shortly after Android 5.0 was released (build LPX13D). So I don't know if the problem is tied to my code, to this device, or to bugs in Android.
我有同样的示例应用程序的两个版本:
I have two editions of the same sample application:
-
一个使用
NotificationCompat
和NotificationManagerCompat
其他使用通知
和 NotificationManager
与的minSdkVersion
21和 targetSdkVersion $ C $的C 21>
(注意,这些项目主要是为Android Studio中使用; Eclipse用户可以导入的项目,但他们可能需要轻微的修正,特别是对引用的支持-V13
库的第一样本)
(note that these projects are primarily for use within Android Studio; Eclipse users can import the projects but they may require minor fixups, particularly for references to the support-v13
library for the first sample)
本示例使用 AlarmManager
触发通知
的工作,主要是让你有机会得到回锁屏看到的结果。这里是的BroadcastReceiver
由 AlarmManager
触发(显示 NotificationCompat
版本):
The samples use AlarmManager
to trigger the Notification
work, mostly so you have a chance to get back to the lockscreen to see the results. Here is the BroadcastReceiver
that is triggered by AlarmManager
(showing the NotificationCompat
version):
/***
Copyright (c) 2014 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
http://commonsware.com/Android
*/
package com.commonsware.android.lollipopnotify;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
public class AlarmReceiver extends BroadcastReceiver {
private static final int NOTIFY_ID=1337;
static final String EXTRA_TYPE="type";
@Override
public void onReceive(Context ctxt, Intent i) {
NotificationManagerCompat mgr=NotificationManagerCompat.from(ctxt);
switch (i.getIntExtra(EXTRA_TYPE, -1)) {
case 0:
notifyPrivate(ctxt, mgr);
break;
case 1:
notifyPublic(ctxt, mgr);
break;
case 2:
notifySecret(ctxt, mgr);
break;
case 3:
notifyHeadsUp(ctxt, mgr);
break;
}
}
private void notifyPrivate(Context ctxt, NotificationManagerCompat mgr) {
Notification pub=buildPublic(ctxt).build();
mgr.notify(NOTIFY_ID, buildNormal(ctxt).setPublicVersion(pub).build());
}
private void notifyPublic(Context ctxt, NotificationManagerCompat mgr) {
mgr.notify(NOTIFY_ID,
buildNormal(ctxt)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.build());
}
private void notifySecret(Context ctxt, NotificationManagerCompat mgr) {
mgr.notify(NOTIFY_ID,
buildNormal(ctxt)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.build());
}
private void notifyHeadsUp(Context ctxt, NotificationManagerCompat mgr) {
mgr.notify(NOTIFY_ID,
buildNormal(ctxt)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build());
}
private NotificationCompat.Builder buildNormal(Context ctxt) {
NotificationCompat.Builder b=new NotificationCompat.Builder(ctxt);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setContentTitle(ctxt.getString(R.string.download_complete))
.setContentText(ctxt.getString(R.string.fun))
.setContentIntent(buildPendingIntent(ctxt, Settings.ACTION_SECURITY_SETTINGS))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setTicker(ctxt.getString(R.string.download_complete))
.addAction(android.R.drawable.ic_media_play,
ctxt.getString(R.string.play),
buildPendingIntent(ctxt, Settings.ACTION_SETTINGS));
return(b);
}
private NotificationCompat.Builder buildPublic(Context ctxt) {
NotificationCompat.Builder b=new NotificationCompat.Builder(ctxt);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setContentTitle(ctxt.getString(R.string.public_title))
.setContentText(ctxt.getString(R.string.public_text))
.setContentIntent(buildPendingIntent(ctxt, Settings.ACTION_SECURITY_SETTINGS))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.addAction(android.R.drawable.ic_media_play,
ctxt.getString(R.string.play),
buildPendingIntent(ctxt, Settings.ACTION_SETTINGS));
return(b);
}
private PendingIntent buildPendingIntent(Context ctxt, String action) {
Intent i=new Intent(action);
return(PendingIntent.getActivity(ctxt, 0, i, 0));
}
}
在 EXTRA_TYPE
正在从微调
的活动设置。这个逻辑似乎是确定的,因为抬头通知
情况下工作得很好。如果我通过code步骤(例如,断点的onReceive()
),我看到它通过正确的路径(例如,调用 setVisibility(NotificationCompat.VISIBILITY_SECRET)
在 notifySecret()
时,我选择加一个秘密通知
)。
The EXTRA_TYPE
is being set from a Spinner
in the activity. That logic seems to be OK, because the heads-up Notification
scenario works just fine. And if I step through the code (e.g., breakpoint in onReceive()
), I see it going through the right paths (e.g., calling setVisibility(NotificationCompat.VISIBILITY_SECRET)
in notifySecret()
when I choose to raise a secret Notification
).
因此,我在一个小的损失的,为什么我没有收到对Android 5.0的可视性效果锁屏。
Hence, I'm at a bit of a loss as to why I am not getting the visibility effects on the Android 5.0 lockscreen.
有什么建议?
您所描述的行为与我的经验,当我把我的锁屏通知preference的行为是一致的显示所有通知的内容。
The behavior you are describing is consistent with the behavior I experience when I set my lockscreen notification preference to "show all notification content."
该设置有三个选项:
-
显示所有通知内容让所有的通知(不论知名度),有效地公开。
Show all notification content makes all notifications (regardless of visibility) effectively public.
隐藏敏感的通知内容尊重新的可见性类型。
Hide sensitive notification content respects the new visibility types.
不要在所有显示通知将使所有的通知有效的秘密。
Don't show notifications at all will make all notifications effectively secret.
要改变你的锁屏通知可视性的选项在声音和放大器在设备设置;通知>设备已被锁定,如下图所示。
The option to change your lockscreen notification visiblity is in the device settings under Sound & Notifications > "When device is locked", as shown below.
由于 Selvin在他的回答指出,如果设置某种隐藏敏感内容的选项才可用设备锁(例如PIN或图案锁)。如果你能与锁屏简单刷卡解锁设备,此选项不可用。
As Selvin noted in his answer, the option to hide sensitive content is only available if you have set some sort of device lock (such as a PIN or pattern lock). If you can unlock your device with a simple swipe of the lockscreen, this option is not available.