2017-05-26 34 views
0

部分手機不支持大型通知。那麼我怎麼能在運行時知道我的notif大小或平常大小?另外我需要它支持控制按鈕。 這裏是我的大通知符代碼:Android小型和大型通知

RemoteViews remoteViews = new RemoteViews(getPackageName(), 
      settings.getAttributeId(settings.getThemeByString(), R.attr.theme_dependent_notification)); 

    remoteViews.setOnClickPendingIntent(R.id.prev, ppreviousIntent); 
    remoteViews.setOnClickPendingIntent(R.id.play, pplayIntent); 
    remoteViews.setOnClickPendingIntent(R.id.next, pnextIntent); 
    remoteViews.setOnClickPendingIntent(R.id.close, pcloseIntent); 

    remoteViews.setImageViewBitmap(R.id.cover, icon); 
    remoteViews.setTextViewText(R.id.name, title); 
    remoteViews.setTextViewText(R.id.artist, text); 
    remoteViews.setTextViewText(R.id.count, count); 

    if (isPlayerActive) remoteViews.setImageViewResource(R.id.play, R.drawable.pause); 
    else remoteViews.setImageViewResource(R.id.play, R.drawable.play); 

    notification = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.equalizer_icon) 
      .setContentTitle(title) 
      .setContentText(text) 
      .setTicker(getString(R.string.app_name)) 
      .setContentIntent(pendingIntent) 
      .setPriority(Notification.PRIORITY_MAX) 
      .build(); 

    notification.bigContentView = remoteViews; 

回答

0

試試這個代碼:

public static void showNotification(Context context) { 
    int notificationId = 99; 
    Intent cancelIntent = new Intent(YOUR_ACTION).putExtra(YOUR_EXTRA_NOTIFICATION_ID, notificationId); 
    PendingIntent buttonPendingIntent = PendingIntent.getBroadcast(context, 123, cancelIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder notBuilder = new NotificationCompat.Builder(context) 
      .setAutoCancel(false) 
      .setOngoing(true) 
      .setPriority(NotificationCompat.PRIORITY_HIGH) 
      .setSmallIcon(android.R.drawable.stat_notify_sync) 
      .setTicker("Ticker title") 
      .setOnlyAlertOnce(true) 
      .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, YourActivity.class), PendingIntent.FLAG_UPDATE_CURRENT)); 

    Notification not = notBuilder 
      .setContentTitle("Title") 
      .setContentText("Message") 
      .setProgress(100, 33, false) 
      .addAction(android.R.drawable.ic_menu_close_clear_cancel, "Cancel", buttonPendingIntent) 
      .setStyle(new NotificationCompat.BigTextStyle().bigText("Message")) 
      .build(); 

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(notificationId, not); 
} 

它顯示一個按鈕,一個大的通知 - 您可以通過拉動底邊向下放大。

+0

但是自定義佈局呢?我需要notif的變種:對於手機來說,它可以顯示大而平常的手機尺寸,而手機不能。自定義佈局上帶有控制按鈕的通知 –

+0

您是否試過https://stackoverflow.com/questions/21237495/create-custom-big-notifications或https://stackoverflow.com/questions/18367631/change-notification-layout? – isabsent

+0

你不瞭解我。我知道如何進行自定義佈局。你可以看到我的代碼。當不允許的情況下,我不知道如何顯示通常尺寸的notif。 –