package com.mn.keeplive.service;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

public class ForegroundService extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        //如果机型是4.3以下
        if(Build.VERSION.SDK_INT<Build.VERSION_CODES.JELLY_BEAN_MR2){
            startForeground(1,new Notification());
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            //开启一个通知，然后又去关闭它
            startForeground(1,new Notification());
            startService(new Intent(this,InnerService.class));
        }
    }
    public static class InnerService extends Service {

        @Override
        public void onCreate() {
            super.onCreate();
            Log.e("TAG", "InnerService 服务创建了");
            startForeground(1, new Notification());
            stopSelf();
        }

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

}
