如何使用minSdkVersion=";为android项目生成通知;8“;和targetSdkVersion=";17“;
我正在尝试生成通知。在这里,我面临的问题是,对于更高的API级别,使用不推荐的方法,当我要使用Notification.Builder()时,它会给出错误提示 调用需要API级别11 这是我的密码如何使用minSdkVersion=";为android项目生成通知;8“;和targetSdkVersion=";17“;,android,android-notifications,Android,Android Notifications,我正在尝试生成通知。在这里,我面临的问题是,对于更高的API级别,使用不推荐的方法,当我要使用Notification.Builder()时,它会给出错误提示 调用需要API级别11 这是我的密码 public void generateNotification(Context context, String message,String id) { int icon = R.drawable.notiIcon; String title = context.ge
public void generateNotification(Context context, String message,String id) {
int icon = R.drawable.notiIcon;
String title = context.getString(R.string.app_name);
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context,
NotificationViewActivity.class);
notificationIntent.putExtra("msg", message);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message,
intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
//here i have Notification.Builder ... that gives error API leve
/*
notification = new Notification.Builder(context)
.setContentTitle(title).setContentText(message)
.setSmallIcon(icon).build();
*/
notificationManager.notify(id, notification);
}
如何解决这个问题。。因为我需要min version 8和target 17。请改用。来自标准支持库v4
顺便说一下,
targetSdkVersion
在大多数情况下都不是一个有问题的参数,在这个问题中不需要指定它。只要试着一直使用最新的API(目前是19个)。使用不推荐的方法没有错,没有坏处。
此外,您还可以使用
if (android.os.Build.VERSION.SDK_INT > 10) {
}
else {
}
如果您检查是否在仍然支持这些方法的SDK版本上使用这些方法,那么使用不推荐的方法没有什么错。这些方法可以在未来版本中删除,这样你的应用程序就可以停止在未来版本的Android上工作。(顺便说一句,被否决的不是我)谢谢抗坏血酸。。像这样的东西我已经试过了,但它在其他部分出现了错误。。好的,NotificationCompat.Builder解决了这个问题。谢谢你的答复谢谢。。这就是我要找的