【问题标题】:PendingIntent doesnt workPendingIntent 不起作用
【发布时间】:2014-12-23 17:14:18
【问题描述】:

我有一项服务会显示通知。通知正在正确显示,但是当我单击通知时没有任何反应。它应该在我的应用程序中打开一个活动。这是我的代码:

public class ServiceCheckExpensesRecurring extends Service{

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            int mNotificationId = 001;
            Intent myIntent = new Intent(this, MenuDashboard.class);
            myIntent.setAction(Intent.ACTION_VIEW);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            PendingIntent pIntent = PendingIntent.getActivity(getBaseContext(), 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder n  = new NotificationCompat.Builder(this)
                    .setContentTitle("New mail from " + "test@gmail.com")
                    .setContentText("Subject")
                    .setSmallIcon(R.drawable.common_signin_btn_icon_light)
                    .setContentIntent(pIntent)
                    .setAutoCancel(true);

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(mNotificationId, n.build());
            return Service.START_NOT_STICKY;
        }
    }

仅供参考,MenuDashboard 是一个片段(如果有帮助的话)。我正在使用我的索尼 experia mini 和 ICS

【问题讨论】:

标签: android android-pendingintent


【解决方案1】:

Intent 可以启动 Activity(或服务),但不能启动 Fragment。

如果MenuDashboard 是一个片段,那么它将不起作用。

您应该改用 Activity,并将 Fragment 嵌入其中。

【讨论】:

  • 哎哟。从来不知道。 android至少应该抛出一个错误。所以没有办法从通知中加载片段?
  • ermm 如何在活动中嵌入片段?我需要启动片段的原因是因为我的应用程序使用导航抽屉,菜单内的所有活动都是片段
  • @imin 在这种情况下,您只需将 Intent 重新传递给您的 Activity(有抽屉的那个),然后实现 onNewIntent()
  • 酷...我第一次听说 onNewIntent()。但我只是用谷歌搜索了“onNewIntent 调用片段”,我认为我没有找到任何可以帮助我从我的活动中调用片段的结果。你能给我指出正确的方向吗?
  • @imin 您应该能够在其中添加片段的位置包含相同的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多