2014-11-14 83 views
16

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

  • The other使用NotificationNotificationManager與21 minSdkVersion和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鎖屏上的可見性效果。

有什麼建議嗎?

回答

8

您描述的行爲與我將鎖屏通知首選項設置爲「顯示所有通知內容」時遇到的行爲一致。

此設置有三個選項:

  • 顯示所有通知內容使所有的通知(不論知名度),有效地公開。

  • 隱藏敏感通知內容尊重新的可見性類型。

  • 根本不顯示通知將使所有通知有效保密。

選項改變你的鎖定屏幕通知可視性是在通知>「當設備處於鎖定狀態」,如下所示聲音&下的設備設置。

Selvin noted in his answer一樣,隱藏敏感內容的選項僅在您設置了某種設備鎖定(例如PIN或模式鎖定)時纔可用。如果您只需輕掃一下鎖屏即可解鎖設備,此選項不可用。

+0

這很有趣,雖然我沒有「隱藏敏感通知內容」作爲選項。當點擊「設備被鎖定時」偏好時,我有「顯示所有通知內容」和「根本不顯示通知」作爲我唯一的兩個選擇。 – CommonsWare 2014-11-14 15:23:58

+0

正如Selvin的答案所述,如果您設置了PIN或密碼(或顯然是模式),則會顯示「隱藏敏感通知內容」。 – CommonsWare 2014-11-14 15:30:14

+0

嘿嘿,我只是想知道,如果它是在文檔/手冊:) – Selvin 2014-11-14 15:31:44

0

除了Tanis.7x的回答是:你必須選擇任何鎖屏後衛比刷卡其他「隱藏敏感通知內容」選項,會出現聲音&下通知

+0

只有擁有PIN或密碼時纔會顯示「隱藏敏感通知內容」選項。但是,如果只是輕掃,用戶可以根據鎖屏上顯示的內容選擇「全部」或「無」,而不是「隱藏敏感」的中間地帶。 – CommonsWare 2014-11-14 15:28:56

+0

...或圖案... – Selvin 2014-11-14 15:29:10

0

設置NotificationBuilder#setPriority(Notification.PRIORITY_MIN);也可防止在鎖定屏幕上顯示通知。

令人驚訝,但至少它是有據可查的。

+1

不,AFAIK你仍然可以在鎖定屏幕上向下滑動並查看所有PRIORITY_MIN通知。 – EyesClear 2015-08-19 17:37:40

相關問題