2014-11-14 89 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

相关问题