error when building a notification

2019-05-13 18:26发布

I am using this tutorial http://www.vogella.com/articles/AndroidNotifications/article.html , I want to build notification

// Prepare intent which is triggered if the // notification is selected

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

I have error in the line addAction , eclipse says that the method addaction(ing, String pendingintent) is undefined for the type notification.builder

3条回答
戒情不戒烟
2楼-- · 2019-05-13 19:03

you have to use android-support-v4.jar file in yout project lib folder /lib/android-support-v4.jar and add it

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

  2. your ptoject---->properites (Alt+)-->Build Path-->Order & Export--> select all--> ok.

using this your code run in <= 11 API LEVEL.


Edited: you are getting error because below method require MIN 11 API level

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

and below method require 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)

so your solution is below use this way:

enter image description here

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

Make sure you have added android-support-v4.jar jar file

查看更多
劫难
3楼-- · 2019-05-13 19:03

You are using a bad version of the support library. AddAction has been introduced in r13 (maybe earlier).

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-05-13 19:11

You need to set your sdkTargetVersion in the <uses-sdk> element of AndroidManifest.xml to be 16 or higher. The addAction(...) method doesn't exist for older versions of Android.

查看更多
登录 后发表回答