3

我正在使用本教程http://www.vogella.com/articles/AndroidNotifications/article.html,我想构建通知

// 准备在选择通知时触发的意图

Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
        .setContentTitle("New mail from " + "test@gmail.com")
        .setContentText("Subject")
        .setSmallIcon(R.drawable.icon)
        .setContentIntent(pIntent)
        .addAction(R.drawable.icon, "Call", pIntent)
        .addAction(R.drawable.icon, "More", pIntent)
        .addAction(R.drawable.icon, "And more", pIntent).build();


NotificationManager notificationManager = 
  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, noti); 

我有错误addAction,日食说the method addaction(ing, String pendingintent) is undefined for the type notification.builder

4

3 回答 3

0

你必须在你android-support-v4.jar的项目 lib 文件夹中使用文件/lib/android-support-v4.jar并添加它

  1. 你的 ptoject---->properites (Alt+)-->Build Path-->Library-->add jar-->select android-support-v4.jarfrom lib folder --> ok

  2. 你的 ptoject---->properites (Alt+)-->Build Path-->Order & Export--> 全选--> ok。

使用这个你的代码在 <= 11 API LEVEL 中运行。


已编辑: 您收到错误,因为以下方法需要 MIN 11 API 级别

.setContentTitle("My notification")
.setContentText("Hello World!");

以下方法需要 MIN API LEVEL 16:

.addAction(R.drawable.ic_launcher, "call", pIntent)
.addAction(R.drawable.ic_launcher, "more", pIntent)
.addAction(R.drawable.ic_launcher, "add more", pIntent)

所以您的解决方案如下使用这种方式:

在此处输入图像描述

Intent intent = new Intent(this, TestSettings.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .addAction(R.drawable.ic_launcher, "call", pIntent)
                .addAction(R.drawable.ic_launcher, "more", pIntent)
                .addAction(R.drawable.ic_launcher, "add more", pIntent)
                .setContentTitle("My notification")
                .setContentText("Hello World!");
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, TestSettings.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

        stackBuilder.addParentStack(TestSettings.class);

        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(100, mBuilder.build());

确保您已添加android-support-v4.jarjar 文件

于 2013-04-02T09:14:33.697 回答
0

您需要将您的sdkTargetVersionin<uses-sdk>元素设置AndroidManifest.xml为 16 或更高。addAction(...)旧版本的 Android 不存在该方法。

于 2013-04-02T09:11:32.090 回答
0

您正在使用错误版本的支持库。AddAction 已在 r13 中引入(可能更早)。

于 2013-07-24T14:24:53.253 回答