Android:无法从通知中启动活动

时间:2012-05-30 08:38:04

标签: android notifications

我有以下代码用于分派通知......

private void publishNotification(Intent intent, String header, String content){
        try {
            NotificationManager manager = getNotificationManager();
            Notification notification = new Notification(R.drawable.droid, header, System.currentTimeMillis());

            if(intent != null){
                PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
                notification.setLatestEventInfo(this, header, content, pendingIntent);
            }

            manager.notify(0, notification);

        } catch (Exception ex) {
            FileManager.writeToLogFile(this.getClass(), "publishNotification", LogMessageType.ERROR, ex.getMessage());
        }
    }

以下代码调用它......

Intent intent = new Intent();
                intent.setAction(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                publishNotification(intent, "Default Password Changed", "Device configuration have changed.\nClick here to change this password.");

我的问题如下......

通知会显示在通知中,甚至在通知滑落后也会显示。问题是当我点击此通知时,活动不会启动。我想我已经完成了tutorial中提到的所有事情。我做错了什么?

2 个答案:

答案 0 :(得分:4)

如果您要发布活动,则应使用PendingIntent.getActivity()代替PendingIntent.getService()

答案 1 :(得分:2)

Intent intent = new Intent(context, class_name.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

并使用PendingIntent.getActivity()代替PendingIntent.getService()