android使用START\u启动前台服务

android使用START\u启动前台服务,android,android-8.0-oreo,background-service,foreground-service,Android,Android 8.0 Oreo,Background Service,Foreground Service,我无法理解android关于服务的新特性。在google文档中,在oreo之后,开发者必须在应用程序位于后台时使用前台服务来启动服务 我找到了这些描述 从Android O开始,如果你的应用程序在后台(检查以上三个条件),你的应用程序可以创建并运行后台服务几分钟 几分钟后,应用程序将进入空闲阶段。当您的应用程序进入空闲阶段时,系统将停止所有后台服务,就像您的服务调用service.stopSelf() 我不明白,即使我用start_STICKY启动服务,它不会再次启动吗?我知道如果我从start

我无法理解android关于服务的新特性。在google文档中,在
oreo
之后,开发者必须在应用程序位于后台时使用前台服务来启动服务

我找到了这些描述

从Android O开始,如果你的应用程序在后台(检查以上三个条件),你的应用程序可以创建并运行后台服务几分钟

几分钟后,应用程序将进入空闲阶段。当您的应用程序进入空闲阶段时,系统将停止所有后台服务,就像您的服务调用
service.stopSelf()

我不明白,即使我用start_STICKY启动服务,它不会再次启动吗?我知道如果我从start_STICKY开始,它会在kill后重新启动。为什么我必须使用
JobScheduler
来满足某些需求(位置更新等)。有人能解释一下吗?我不能很好地理解google文档

我现在在galaxy note 8 api26上测试它。我在应用程序启动时使用startService启动服务,在关闭应用程序后重新启动。旧版本之间有什么区别

多谢各位

ublic class MyActivity2 extends Activity {

private Intent serviceIntent;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    serviceIntent = new Intent(this, MyService.class);
    serviceIntent.putExtra("name", "ahmet vefa saruhan");
    startService(serviceIntent);
    findViewById(R.id.textview).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            stopService(new Intent(getApplicationContext(),MyService.class));
        }
    });
}
}


}

主要原因是电池寿命。在旧版本的安卓系统中,开发人员利用了这一问题,使他们的应用程序在设备上不间断运行,并且永不死机,从而耗尽了设备的电池寿命。必须启动前台服务的原因是,用户知道你的应用程序正在运行,并且可以在用户想要的任何时候将其杀死。我得到了它,但是如果我从activity中使用startservice会发生什么?我看到它在应用程序关闭后再次启动。我看不到任何编程问题。如果它是一个前台服务,它可以像以前一样运行。只要在您的服务中,您调用
service.Startforground(新通知…
否则服务将再次停止,可能会使应用程序崩溃。我想你有5秒钟的时间来确认,当应用程序启动时,我已经通过startservice启动了服务,但什么都没有发生。如果我从后台启动,它将抛出错误。好吧,我希望看到所有相关代码能够帮助你。Service代码、启动服务的部分等。
public class MyService extends Service {

private String myaction;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d("MYSERVICE STATUS: "," ONSTARTCOMMAND action : " + ((intent != null && intent.getStringExtra("name") != null) ? intent.getStringExtra("name") : "yok") + " OLD ACTION : "+(myaction != null ? myaction : "yok"));
    Log.d("MYTHREAD name : ",Thread.currentThread().getName());
    //intent.putExtra("name","isim değişti");
    myaction = (intent != null) ? intent.getAction() : null;
    return START_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();
   Log.d("MYSERVICE STATUS: "," ONCREATED");
}

@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground()
{
    String NOTIFICATION_CHANNEL_ID = "example.permanence";
    String channelName = "Background Service";
    NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setContentTitle("App is running in background")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(2, notification);
}

@Override
public void onDestroy() {
    Log.d("MYSERVICE STATUS: "," ONDESTROYED");
    super.onDestroy();
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Log.d("MYSERVICE STATUS: "," onTaskRemoved");
    super.onTaskRemoved(rootIntent);
}