2016-08-30 69 views
10

我正在開發Android應用程序。最後一個使用鎖定屏幕上顯示的具有自定義視圖的通知。不幸的是,當我像其他通知一樣點擊它時,我無法獲得波紋和海拔效果。此外,單一觸摸觸發我配置的意圖,而其他通知需要雙擊。Android鎖定屏幕通知自定義視圖帶紋波和雙擊

我已經把在Github上最小的項目例如:

https://github.com/lpellegr/android-notification-custom-example

該應用程序例如提供兩個按鈕來發布通知:一個使用自定義視圖,然後從上面提到的問題,另一個通知受苦的是使用預期行爲的默認系統視圖。

enter image description here

有關如何獲取紋波和高程的效果,但也雙擊行爲(通過保持自定義視圖)任何想法是值得歡迎的。

PS:我指定的API 19+,我想使用自定義視圖佈局的通知,與setOnClickPendingIntent一起,因爲只有這個監聽器允許打開活動無論設備的安全模式。從方法publishNotificationWithCustomView

回答

3

刪除setOnClickPendingIntent並添加setContentIntent的通知建設者:

private void publishNotificationWithCustomView() { 
    String title = "Notification Custom View"; 
    String content = "No ripple effect, no elevation, single tap trigger"; 
    Context context = getApplicationContext(); 

    NotificationCompat.Builder builder = 
      new NotificationCompat.Builder(context) 
        .setWhen(System.currentTimeMillis()) 
        .setDefaults(DEFAULT_ALL) 
        .setSmallIcon(R.mipmap.ic_launcher) 
        .setPriority(NotificationCompat.PRIORITY_HIGH) 
        .setOnlyAlertOnce(true) 
        .setAutoCancel(false) 
        .setColor(ContextCompat.getColor(context, R.color.colorAccent)) 
        .setContentTitle(title) 
        .setContentText(content) 
        .setOngoing(true) 
        .setCategory(NotificationCompat.CATEGORY_ALARM) 
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) 
        .setContentIntent(createLockscreenNotificationPendingIntent(context)); 

    int notificationLayoutResId = R.layout.lock_screen_notification; 

    // using folder layout-vX is having issue with LG devices 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
     notificationLayoutResId = R.layout.lock_screen_notification_android_n; 
    } 

    RemoteViews remoteView = new RemoteViews(
      context.getPackageName(), notificationLayoutResId); 
    remoteView.setTextViewText(R.id.title, title); 
    remoteView.setTextViewText(R.id.text, content); 

    builder.setCustomContentView(remoteView); 

    Notification notification = builder.build(); 
    publishNotification(context, notification, 7); 
} 

然後從lock_screen_notification.xmllock_screen_notification_android_n.xml刪除android:clickable="true"

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="64dp"> 

    .... 
+2

謝謝你的建議。不幸的是,如果我使用_setContentIntent_而不是_setOnClickPendingIntent_,那麼當設備使用模式,引腳等進行保護時,意圖要求解鎖鎖定屏幕以查看活動。當設置_setOnClickPendingIntent_時,無論安全模式是什麼,該活動都將打開而不解鎖。出於這個原因,你的建議對我無效。 – Laurent

+0

@Laurent你有沒有找到解決方案? – cristianomad