【问题标题】:How to keep alive a service, after starting, until the application be closed or killed in Android?如何在启动后使服务保持活动状态,直到应用程序在 Android 中关闭或终止?
【发布时间】:2018-01-10 21:14:34
【问题描述】:

我正在开发android应用程序,所以我正在启动一个带有警报的服务:

public void scheduleLocationCheckerAlarm() {
    Intent intent = new Intent(getApplicationContext(), LocationCheckerReceiver.class);
    final PendingIntent pIntent = PendingIntent.getBroadcast(this, LocationCheckerReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    long firstMillis = System.currentTimeMillis();
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 600000, pIntent);
}

LocationCheckerReceiver:

public class LocationCheckerReceiver extends BroadcastReceiver {
public static final int REQUEST_CODE = 12345;

@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(context, LocationNotificator.class);
    context.startService(i);
}

服务:

public class LocationNotificator extends Service {
public LocationNotificator() {
}

@Override
public IBinder onBind(Intent intent) {
    throw new UnsupportedOperationException("Not yet implemented");
}

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("Location checker", "Service running");
    //My code is here
    return START_STICKY;
}
@Override
public void onDestroy() {
    super.onDestroy();
    Log.d("Location checker", "Service destroyed");
}

所以我希望该服务每 1 分钟检查一次并一直运行,即使应用程序已被用户关闭。

【问题讨论】:

  • 你遇到了什么问题?
  • 你是否在 Manifest 中声明了服务?
  • 应用程序关闭时服务被杀死 Android manifest:

标签: java android service


【解决方案1】:

您必须致电startForeground(FOREGROUND_ID, buildForegroundNotification(filename)); 以确保您的服务持续运行。此外,这将发布来自您的应用程序的通知,以向用户显示服务状态。请关注reference。 这是代码:

public class LocationNotificator extends Service {
private static int FOREGROUND_ID=1338;
public LocationNotificator() {
}

@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not yet implemented");
}

public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Location checker", "Service running");
//My code is here

startForeground(FOREGROUND_ID,
                buildForegroundNotification(filename));
stopForeground(true);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("Location checker", "Service destroyed");
}
private Notification buildForegroundNotification(String filename) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);

b.setOngoing(true);

b.setContentTitle("Some Title")
 .setContentText("some File name")
 .setSmallIcon(android.R.drawable.stat_sys_download)
 .setTicker("downloading");

return(b.build());
}

【讨论】:

  • 好像听不懂你的意思。我在我的服务上从 public int onStartCommand() 调用了 startForeground,但没有任何反应。
  • 我已经编辑了我的帖子。请按照提供的代码示例进行操作。
  • 我刚试了一下,没有任何区别,通知没有显示,因为某种原因
  • 此服务现在运行时间不长。这项服务可以长期运行吗?如果是,请告诉我如何
【解决方案2】:

您需要先阅读此内容。 https://developer.android.com/guide/components/services.html

简而言之,将您的服务作为前台服务启动,这样 Android 杀死您的服务的机会就会更小。作为前台服务,您需要在状态栏中显示持续通知。

没有直接的方法可以确保您的服务永远不会被 Android 系统杀死。一种解决方法是在您的服务的onDestroy() 中发送一个广播,并在您的Android 应用程序中使用Receiver 在接收到广播后启动该服务。

顺便说一句,您的服务似乎正在定期向您的后端服务器发送位置更新。使用Firebase Job Dispatcher libraryEvernote's Android-Job library 可能会更好地实现这一点。

【讨论】:

  • 我的服务正在向我的后端服务器发送请求,感谢您的回复!
猜你喜欢
  • 1970-01-01
  • 2022-12-22
  • 2011-09-13
  • 1970-01-01
  • 2015-09-20
  • 2016-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多