我正在开发一个在API 7及更高版本上运行的应用程序,因此我必须使用NotificationCompat.Builder
而不是Notification,因为它在较高版本中已弃用。这在模拟器上工作正常,但在我的设备上测试时没有notification
。请有人帮帮我。
注意:是不是只能使用API 7到14的API。我想知道因为我的设备使用API 7
答案 0 :(得分:2)
尝试此功能 - 它适用于Android 2最多4:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
public static void pushNotification(final Context context,
int icon, String name, String descr, Intent activityIntent) {
NotificationManager notifyMgr =
(NotificationManager)context.getSystemService(
Context.NOTIFICATION_SERVICE);
long when = System.currentTimeMillis();
PendingIntent pIntent = PendingIntent.getActivity(
context, 0, activityIntent, 0);
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT < 11)
notification = getNotification8(context,
icon, name, descr, when, pIntent);
else notification = getNotification11(context,
icon, name, descr, when, pIntent);
notifyMgr.notify(NOTIFY_ID, notification);
}
@SuppressWarnings("deprecation")
private static Notification getNotification8(Context context,
int icon, String name, String descr,
long when, PendingIntent pIntent) {
Notification notification = new Notification(icon, name, when);
notification.setLatestEventInfo(context, name, descr, pIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
return notification;
}
@TargetApi(11)
private static Notification getNotification11(Context context,
int icon, String name, String descr,
long when, PendingIntent pInten) {
Notification notification = new Notification.Builder(context)
.setTicker(name)
.setContentTitle(name)
.setContentText(descr)
.setSmallIcon(icon)
.setContentIntent(pInten)
.setAutoCancel(true)
.setWhen(when)
.getNotification();
return notification;
}