找不到符号方法setVisibility(int)

时间:2017-04-09 13:10:07

标签: android push-notification ibeacon-android

我正在尝试构建一种通知方法,当检测到特定信标时,该通知方法会导致锁定屏幕上出现通知。我的理解是我需要在以下代码中包含.setVisibility(0):

public void showNotification(Beacon beacon) {

    Resources r = getResources();
    int random = (int)System.currentTimeMillis();
    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.ic_popup_reminder)
            .setContentTitle("Beacons Found")
            .setContentText(beacon.getID().toString())
            .setVisibility(0)  // allow notification to appear on locked screen
            .setAutoCancel(true)
            .build();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(random, notification);

}

我有上面的代码,但是当我运行它时它会说"找不到符号方法setVisibility(int)"。我在网上做了一些研究,似乎我需要导入这些:

 import android.support.v4.app.NotificationCompat;
 import android.app.Notification;
 import android.app.NotificationManager;

即使我包含这些导入,我仍然会收到相同的错误消息。有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:0)

在Api 21中添加了setVisibility()根据我发现的文档可能会尝试这个。

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {

  Notification notification = new NotificationCompat.Builder(this)
        .setSmallIcon(android.R.drawable.ic_popup_reminder)
        .setContentTitle("Beacons Found")
        .setContentText(beacon.getID().toString())
        .setVisibility(0)  // allow notification to appear on locked screen
        .setAutoCancel(true)
        .build();

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(random, notification);

}