从通知开始活动

时间:2012-06-24 21:16:48

标签: android android-intent notifications

我正在开发一个Android应用,我正在尝试创建通知,一旦用户点击通知,就应该启动一个活动。通知正在创建,但活动未启动。

以下是通知的代码。

private void showNotification(String username, String password)
{
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(ns);

    CharSequence tickerText = "Username Copied";
    long when = System.currentTimeMillis();

    int icon = R.drawable.icon;

    Notification notification = new Notification(icon, tickerText, when);
    CharSequence contentTitle = "BPM - Login Management";
    CharSequence contentText = "Username Copied";

    Intent intentNotification = new Intent(context, CopyPassword.class);
    intentNotification.setAction(Long.toString(when));
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    intentNotification.putExtra("password", password);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotification, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    final int NOTIFICATION_ID = 1;
    notificationManager.notify(NOTIFICATION_ID, notification);
}

如果这会产生任何差异,则会在普通的java类中生成通知而不是android活动。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

删除不需要的setAction。

尝试:

Intent intentNotification = new Intent(context, CopyPassword.class);
intentNotification.putExtra("password", password);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotification, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

还要确保在AndroidManifest

中声明CopyPassword活动

我写了一篇关于它的博客文章:http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ :-D