1

我正在开发一个在 API 7 及更高版本上运行的应用程序,因此我必须使用NotificationCompat.Builder而不是 Notification,因为它在更高版本中已弃用。这在模拟器上运行良好,但在我的设备上测试时没有notification. 请有人可以帮助我。注意:是否不能只使用 API 7 到 14 的 API。我想知道,因为我的设备使用 API 7

4

1 回答 1

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;
    }
于 2013-06-07T09:24:49.670 回答