error when building a notification

时光怂恿深爱的人放手 提交于 2019-12-06 03:44:07

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.

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:

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!