这个概念是在特定时间收到通知。显然,我做到了,直到我添加了对低于以下版本的支持HoneyComb
并在其上方。
我已经设置了最小 SDK 版本 8 和目标 SDK 17。由于类编码要大得多,我仅显示存在问题的核心区域:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
Notification notification;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, TaskDetails.class), 0);
if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
notification = new Notification(icon, text, time);
notification.setLatestEventInfo(this, title, text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(NOTIFICATION, notification);
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(
this);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(icon).setTicker(text).setWhen(time)
.setAutoCancel(true).setContentTitle(title)
.setContentText(text).build();
mNM.notify(NOTIFICATION, notification);
}
问题是,
- 不推荐使用Notifications类的一些方法和构造函数。
- 因此,替代方案是“developer.android.com”建议使用
Notification.Builder
.
- 但是,班级
Notification.Builder
有包含的方法
在 API 级别 11 中(因此显示行中的错误"Call requires API level 11 or more"
).
- 所以,它不允许我运行该项目。
- 经过更多谷歌搜索后,给了我使用类的解决方案
NotificationCompat.Builder
.
最后,到达没有发现错误的阶段,我在我的 Sony Xperia Tipo Dual ST21i2 上运行我的项目......
悲惨的结局:我收到以下错误日志:
06-01 06:34:14.199: E/AndroidRuntime(4178): FATAL EXCEPTION: main
06-01 06:34:14.199: E/AndroidRuntime(4178): java.lang.NoClassDefFoundError: android.support.v4.app.NotificationCompat$Builder
06-01 06:34:14.199: E/AndroidRuntime(4178): at com.todotaskmanager.service.NotifyService.showNotification(NotifyService.java:99)
06-01 06:34:14.199: E/AndroidRuntime(4178): at com.todotaskmanager.service.NotifyService.onStartCommand(NotifyService.java:68)
06-01 06:34:14.199: E/AndroidRuntime(4178): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)
06-01 06:34:14.199: E/AndroidRuntime(4178): at android.app.ActivityThread.access$1900(ActivityThread.java:123)
06-01 06:34:14.199: E/AndroidRuntime(4178): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
06-01 06:34:14.199: E/AndroidRuntime(4178): at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 06:34:14.199: E/AndroidRuntime(4178): at android.os.Looper.loop(Looper.java:137)
06-01 06:34:14.199: E/AndroidRuntime(4178): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-01 06:34:14.199: E/AndroidRuntime(4178): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 06:34:14.199: E/AndroidRuntime(4178): at java.lang.reflect.Method.invoke(Method.java:511)
06-01 06:34:14.199: E/AndroidRuntime(4178): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
06-01 06:34:14.199: E/AndroidRuntime(4178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
06-01 06:34:14.199: E/AndroidRuntime(4178): at dalvik.system.NativeStart.main(Native Method)