首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通知在Emulator上工作,但在我的设备上不起作用

通知在Emulator上工作,但在我的设备上不起作用
EN

Stack Overflow用户
提问于 2013-06-07 17:19:38
回答 1查看 313关注 0票数 1

我正在开发一个在API7和更高版本上运行的应用程序,所以我不得不使用NotificationCompat.Builder而不是通知,因为它在更高的版本中已被弃用。这在模拟器上运行良好,但在我的设备上测试时没有notification。有人能帮帮我吗。注: API 7到14不能只使用API吗?我想知道,因为我的设备使用API 7

EN

回答 1

Stack Overflow用户

发布于 2013-06-07 17:24:50

试试这个函数--它可以在android 2到4上运行:

代码语言:javascript
复制
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;
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16980534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档