Java 在Android中,在应用程序关闭或终止之前,如何使服务在启动后保持活动状态?

Java 在Android中,在应用程序关闭或终止之前,如何使服务在启动后保持活动状态?,java,android,service,Java,Android,Service,我正在开发android应用程序,因此我正在启动一项带有警报的服务: public void scheduleLocationCheckerAlarm() { Intent intent = new Intent(getApplicationContext(), LocationCheckerReceiver.class); final PendingIntent pIntent = PendingIntent.getBroadcast(this, LocationChecker

我正在开发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);
}
位置CheckerReceiver:

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分钟检查一次,并且始终运行,即使用户关闭了应用程序。

您必须调用
startForeground(前台ID,buildForegroundNotification(文件名))以确保您的服务持续运行。此外,这将发布应用程序的通知,向用户显示服务状态。请按照下面的说明。
代码如下:

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());
}

你需要先读一下

简言之,将您的服务作为前台服务启动,这样Android终止您的服务的可能性就更小了。作为前台服务,您需要在状态栏中显示正在进行的通知

没有直接的方法可以确保你的服务不会被Android系统杀死。一种解决方法是在服务的
ondestory()
中发送广播,并在Android应用程序中安装一个
接收器,在收到广播后启动服务


顺便说一下,您的服务似乎正在定期向后端服务器发送位置更新。这可能更好地使用or实现。

您遇到了什么问题?您是否在清单中声明了服务?当应用程序关闭时,服务将被终止安卓清单:似乎我不明白您的意思。我在我的服务上从public int onStartCommand()调用了startForeground,但什么也没发生。我编辑了我的帖子。请遵循提供的代码示例。我刚刚尝试过,没有区别,由于某些原因,通知没有显示此服务现在运行时间不长。这项服务可以长期运行吗?如果是,请让我知道我的服务是如何做的获取请求到我的后端服务器,谢谢回复!