我试图编写一个使用setVisibility()
的演示来控制在Android 5.0锁屏上显示Notification
的内容。然而,似乎没有任何影响:棒棒堂通知setVisibility()不起作用?
默认
VISIBILITY_PRIVATE
仍显示私人Notification
,而不是它的公共对口VISIBILITY_SECRET
通知仍然显示在锁定屏幕上
督察,所有的行为都像VISIBILITY_PUBLIC
有效,至少当我测试运行Android 5.0图像的Nexus 7时,我们在Android 5.0发布后立即给出(构建LPX 13D)。所以我不知道问题是否与我的代码,这个设备或Android中的错误有关。
我具有相同的样本应用程序的两个版本:
One使用
NotificationCompat
和NotificationManagerCompat
The other使用
Notification
和NotificationManager
与21minSdkVersion
和21
targetSdkVersion
(请注意,这些项目主要用于Android Studio; Eclipse用户可以导入项目,但他们可能需要轻微的修正,特别是第一个样品)到support-v13
库引用
样品使用AlarmManager
触发Notification
工作,主要是让你有机会得到回锁屏以查看结果。这里是受AlarmManager
触发(表示NotificationCompat
版本)的BroadcastReceiver
:
/***
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
正在从在活性的Spinner
设置。这种逻辑似乎是好的,因为单挑Notification
方案工作得很好。如果我单步执行代码(例如,onReceive()
中的断点),我会看到它正在通过正确的路径(例如,当我选择提出秘密Notification
时,在notifySecret()
中调用setVisibility(NotificationCompat.VISIBILITY_SECRET)
)。
因此,我有点不知所措,为什么我没有获得Android 5.0锁屏上的可见性效果。
有什么建议吗?
这很有趣,虽然我没有“隐藏敏感通知内容”作为选项。当点击“设备被锁定时”偏好时,我有“显示所有通知内容”和“根本不显示通知”作为我唯一的两个选择。 – CommonsWare 2014-11-14 15:23:58
正如Selvin的答案所述,如果您设置了PIN或密码(或显然是模式),则会显示“隐藏敏感通知内容”。 – CommonsWare 2014-11-14 15:30:14
嘿嘿,我只是想知道,如果它是在文档/手册:) – Selvin 2014-11-14 15:31:44