【问题标题】:android: how to call activity by clicking foreground service notificationandroid:如何通过单击前台服务通知来调用活动
【发布时间】:2017-03-23 13:05:38
【问题描述】:

我启动了一个显示通知的前台服务。如果我的活动被隐藏,我希望通过单击通知开始。 在 onStartCommand 中调用的函数会这样做:

startForeground(noti_id, mNoti);

通知出现并且有效,但它不会重新激活我的 MainActivity:

notiIntent = new Intent(this, MainGate.class);
notiPendingIntent = PendingIntent.getActivity(this, 0, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT);

MainGate.class 是启动前台服务的活动。当我点击通知时它应该会出现。

编辑:

实际上,当通知构建在 man 活动 (MainGate.class) 中时,它就起作用了。当通知内置在不是前台服务的服务中时,它就起作用了。现在我不得不实现前台服务并且它停止工作了。

【问题讨论】:

    标签: android notifications foreground


    【解决方案1】:

    试试这个解决方案

    Intent notificationIntent = new Intent(this, MainGate.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(this, 0,
            notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    
    Notification notification=new NotificationCompat.Builder(this)
                                .setSmallIcon(R.drawable.ic_launcher)
                                .setContentText(getString(R.string.isRecording))
                                .setContentIntent(pendingIntent).build();
    
    startForeground(NOTIFICATION_ID, notification);
    

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 2022-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      相关资源
      最近更新 更多