代码之家  ›  专栏  ›  技术社区  ›  Gilian Marques

启动服务/foregroundService将应用程序置于后台

  •  0
  • Gilian Marques  · 技术社区  · 6 年前

    Intent serviceIntent = new Intent(this, NotificationService.class); startService(serviceIntent);

    如果应用程序在后台运行,则会将其置于前端;如果应用程序已关闭,则不会发生任何事情。我想避免这种行为,但是,我不知道怎么做。 发生在安卓系统上 第页 . alarmManager 在特定时间唤醒我的应用程序,然后 pendingIntent 启动一个 Activity (它在屏幕上不显示任何内容)并检查从 Intent 以确定要执行的操作。我有两个服务在我的应用程序和他们都带来了应用程序,每次他们醒来,这是恼人的用户。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Gilian Marques    6 年前

    我通过更换 PendingIntent.getActivity() 通过 PendingIntent.getBroadcast() 在创建 PendingIntent 会被 AlarmManager . 代码如下:

          private PendingIntent getNotifyIntent() {
             Intent notifyIntent = new Intent(mContext, MyBroadcastReceiver.class);
             return PendingIntent.getBroadcast(mContext, 0, notifyIntent, 0);
    }
    

    我以前就用过这个代码( 正在挂起。getActivity()

    恢复:问题不在于服务本身,而是我设置 全局定时器

    把你们都救出来!

        2
  •  -1
  •   Jeeva    6 年前

    在Android 8.0之前,创建前台服务的通常方法是 若要创建后台服务,请将该服务提升到 前景。对于Android 8.0,有一个复杂的问题;系统 不允许后台应用程序创建后台服务。为了 因此,Android 8.0引入了新的方法 startForegroundService()在前台启动新服务。 系统创建服务后,应用程序有五秒钟的时间 用户可见的通知。如果应用程序不调用startForeground() 在时间限制内,系统停止服务并声明

    https://developer.android.com/about/versions/oreo/background
    https://stackoverflow.com/a/47611385/6059583
    按照此链接显示如何显示NotiLocation以运行service i后台