我试图让我的应用程序发出通知,但由于min api级别为11,我必须使用此代码:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
// call something for API Level 16+
Notification noti;
noti = new Notification.Builder(this)
.setContentTitle("New Notification from LCD")
.setContentText("content")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Option1", pIntent)
.addAction(R.drawable.ic_launcher, "Option2", pIntent)
.addAction(R.drawable.ic_launcher, "Option3",
pIntent).build();
NotificationManager nmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide notification
noti.flags |= Notification.FLAG_AUTO_CANCEL;
nmanager.notify(0, noti);
}
我有另一个设置API级别低于16,使用.getNotification()而不是.build()。
即使在那之后我也收到Call requires API level 16 (current min is 11)
错误。我该怎么做才能解决这个问题?
编辑:忘记添加,.getNotification行显示黄色错误The method getNotification() from the type Notification.Builder is deprecated
答案 0 :(得分:0)
您应该使用@SuppressLint("NewApi")
注释。
例如,下面的方法检测设备是否具有物理菜单按钮:
@SuppressLint("NewApi")
public static boolean hasMenuButton(Context ctx) {
return (
Build.VERSION.SDK_INT <= 10 ||
(Build.VERSION.SDK_INT >= 14 && ViewConfiguration.get(ctx).hasPermanentMenuKey())
);
}
编辑(进一步到OP的编辑):
同样,您可以使用@SuppressWarnings("deprecation")
注释删除弃用警告。例如,下面的方法使用API11下所需的一些代码,但在之后弃用。
@SuppressWarnings("deprecation")
private void initializeSurfaceHolder() {
final SurfaceHolder holder = getHolder();
holder.addCallback(this);
if (Build.VERSION.SDK_INT < 11) {
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}