我正在使用本教程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
中有错误,eclipse说the method addaction(ing, String pendingintent) is undefined for the type notification.builder
答案 0 :(得分:0)
您需要将sdkTargetVersion
<uses-sdk>
元素中的AndroidManifest.xml
设置为16或更高。旧版Android版不存在addAction(...)
方法。
答案 1 :(得分:0)
你必须在你的项目lib文件夹android-support-v4.jar
中使用/lib/android-support-v4.jar
文件并添加它
你的ptoject ----&gt;本地(Alt +) - &gt;构建路径 - &gt;库 - &gt;添加jar - &gt;从lib文件夹中选择android-support-v4.jar
- &GT;确定
您的ptoject ----&gt;专有权(Alt +) - &gt;构建路径 - &gt;订单&amp;出口 - &GT;全选 - &gt;确定。
使用此代码运行在&lt; = 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.jar
jar文件
答案 2 :(得分:0)
您使用的是支持库的错误版本。在r13中引入了AddAction(可能更早)。