【问题标题】:Pending Intent not launching待定意图未启动
【发布时间】:2011-09-06 21:24:13
【问题描述】:

我有一个创建通知的活动。当我使用和 AVD 模拟器(针对 Android 2.1 更新 1)时,通知将很好地启动 PendingIntent,但在实际设备(运行 Android 2.2.1)上根本不会启动。有什么我缺少的基本内容吗?

【问题讨论】:

    标签: android android-pendingintent


    【解决方案1】:

    您尚未发布任何代码。所以我分享了对我有用的代码:

    public static void notifyIcon(Context context){
    
    NotificationManager notifier = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.your_app_notification, "", System.currentTimeMillis());
    
    /**
    *Setting these flags will stop clearing your icon
    *from the status bar if the user does clear all 
    *notifications.
    */
        notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
        notification.contentView = contentView;
    
    //If you want to open any activity on the click of the icon.
    
        Intent notificationIntent = new Intent(context, YourActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;
    
        notification.setLatestEventInfo(context, "title", null, contentIntent);
        notifier.notify(1, notification);
    
    //To cancel the icon ...
        notifier.cancel(1);
    
    }
    

    这里是 custom_notification_layout.xml

    <?xml version = "1.0" encoding = "utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="3dp" >
    
    </LinearLayout>
    

    注意:如果您设置了上述标志,请不要忘记在适当的时候清除您的应用图标。

    【讨论】:

    • 如果您将notifier.cancel() 留在代码中,是否会显示通知?我的代码几乎相同,除了我的 PendingIntent 设置了 FLAG_UPDATE_CURRENT 并且我的 Notification 设置了 FLAG_AUTO_CANCEL 。而且我不会取消我的 NotificationManager
    • notifier.cancel(1);用于删除通知,如果您不想删除,请离开它...
    猜你喜欢
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多