我们可以在android中使用哪些替代方法进行通知?

时间:2013-12-18 09:31:57

标签: android notifications

我有问题。我想要做的是,我只想在频繁的间隔时间在通知栏中显示一条消息。为此,我使用了两种通知方法:

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

目前我正在使用API​​级别19.所以我开始知道上面的那些已被弃用。我被建议使用Notification.builder。但使用后我没有得到正确的输出。谁能告诉我代码如何使用Notification.Builder进行上述2个语句... 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

你可以用这个......

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

答案 1 :(得分:0)

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

这是一个例子:Notification.Builder

设置您可以使用方法setWhen(long timestamp);

的时间