我試圖編寫一個使用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