【问题标题】:PendingIntent on Notification foreground service android通知前台服务android上的PendingIntent
【发布时间】:2013-12-10 23:34:55
【问题描述】:

我的应用中有一个活动。当我单击一个选项时,开始运行我的服务前台并显示如下通知:

PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
            new Intent(getApplicationContext(), Radio.class),
            PendingIntent.FLAG_UPDATE_CURRENT);

    Notification  notification = new Notification();
    notification.tickerText = "Connecting RadiO!.. Wait..";
    notification.icon = R.drawable.ic_launcher;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notification.setLatestEventInfo(getApplicationContext(), "radiO!",
            "Playing: " + songName, pi);
    startForeground(1, notification);

然后,我仍然在运行我的活动,我点击了我的通知并打开了它,而没有关闭之前的活动。我不能有两次相同的活动。

【问题讨论】:

标签: android service notifications android-pendingintent


【解决方案1】:

您所做的是在每次单击通知时创建您尝试执行的活动的新实例,为了避免它并重用已经存在的活动(如果有),只需将该活动设置为 singleTask在AndroidManifest.xml中如下图:

    <activity
       android:name=".YourActivity"
       android:launchMode="singleTask"/>

希望这会有所帮助。

问候!

【讨论】:

  • 当我点击通知时,启动新活动并关闭通知
  • 工作吧!!谢谢,我的应用也需要这个:android:alwaysRetainTaskState="true"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 2017-11-23
  • 1970-01-01
相关资源
最近更新 更多