2013-12-18 46 views
0

我有一個問題。我想要做的是,我只想在頻繁的時間間隔在通知欄中顯示一條消息。爲此,我使用了兩種通知方法:什麼替代方法,我們可以用於在android通知?

Notification notification = new Notification(icon, message, when); 
...... 
notification.setLatestEventInfo(context, title, subTitle, intent); 

目前我使用的是API級別19.因此,我開始瞭解上述情況。我被建議使用Notification.builder。但使用後,我沒有得到正確的輸出。任何人都可以告訴我代碼如何使用Notification.Builder 2以上的語句... 任何幫助將不勝感激。

回答

1

您可以使用此...

public static void createNotification(Context context, Long data) { 

     Random rnd = new Random(); 
     int i = rnd.nextInt(); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.app_icon) 
       .setContentTitle("Logo").setContentText("text"); 
     mBuilder.setAutoCancel(true); 
     // Creates an explicit intent for an Activity in your app 
     Intent resultIntent = new Intent(context, activity.class); 

     resultIntent.putExtra(StringUtils.SESSIONID, data); 
     // The stack builder object will contain an artificial back stack for 
     // the 
     // started Activity. 
     // This ensures that navigating backward from the Activity leads out of 
     // your application to the Home screen. 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 

     // Adds the back stack for the Intent (but not the Intent itself) 
     stackBuilder.addParentStack(activity.class); 
//  stackBuilder.editIntentAt(index); 
     // Adds the Intent that starts the Activity to the top of the stack 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT); 
     mBuilder.setContentIntent(resultPendingIntent); 
     NotificationManager mNotificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     // mId allows you to update the notification later on. 
     mNotificationManager.notify(i, mBuilder.build()); 
+0

NotificationCompact.Builder是降低API水平。如何使用Notification.Builder用於上述語句(有問題)? – user3057567

0
Notification noti = new Notification.Builder(mContext) 
     .setContentTitle("New mail from " + sender.toString()) 
     .setSubText(subTitle) 
     .setContentIntent(pendingIntent) 
     .build(); 

這是例子就是從這裏開始:Notification.Builder

要設定時間,你可以使用的方法setWhen(long timestamp);

+0

是的我試過這個例子,但如何使用它的第二個陳述? – user3057567

+0

看到我編輯的答案:你用'setContentTitle'設置標題,然後使用'setSubText'設置字幕,然後設置待處理意圖'setContentIntent',通過構造函數參數 – Hitman

+0

發送上下文第一條語句工作正常。在第二條語句中,我們可以直接使用通知來使用上述方法嗎 – user3057567

相關問題