2017-04-09 72 views
0

我试图建立一个通知方法,当检测到一个特定的信标时,会导致通知出现在锁定的屏幕上。我的理解是,我需要在下面的代码包括.setVisibility(0):找不到符号方法setVisibility(int)

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; 

即使我有这些进口,我仍然得到同样的错误消息。谁能帮忙?谢谢!!

回答

0

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

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); 

} 
+0

作为他用NotificationCompat,他应该定位API 22所以你应该改变你的版本代码LOLLIPOP_MR1 :) –

+0

感谢这两个,我试图添加上面的代码,但它说“找不到符号变量棒棒糖” .. ..我需要做些什么来解决这个问题? –

+0

它不应该得到错误。请同步项目并清理然后重建项目。如果错误,那么你得到什么gradle错误? – Queendevelopers

相关问题