待定意图未启动

3

我有一个创建通知的活动。当我使用AVD模拟器(针对Android 2.1更新1)时,通知可以正常启动PendingIntent,但在实际设备上(运行Android 2.2.1)根本无法启动。我是否遗漏了一些基本的东西?

1个回答

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>

注意:如果您设置了上述标志,请在适当时清除您的应用程序图标。

1
如果你在代码中留下了notifier.cancel(),通知是否仍会显示?我的代码几乎相同,除了我的PendingIntent设置了 FLAG_UPDATE_CURRENT ,我的Notification设置了 FLAG_AUTO_CANCEL 。并且我没有取消我的NotificationManager。 - vosmith
notifier.cancel(1); 用于移除通知,如果您不想移除它,则可以不执行此操作... - Vineet Shukla

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接