Android 当应用程序关闭时,保持后台运行,就像通知中的Whataps消息一样

Android 当应用程序关闭时,保持后台运行,就像通知中的Whataps消息一样,android,Android,当我打开应用程序时,startService会发送通知,但如果我关闭了我的应用程序,则永远不要停止应用程序并继续运行我的应用程序,就像手机短信一样,如果你关闭了短信应用程序但仍在运行 输出是当我打开应用程序并保持运行时,如果我的应用程序关闭,则服务会自动停止 我想继续运行我的应用程序您是否尝试过使用Foreground服务?如果我的应用程序已关闭,请继续运行,或者在最近的应用程序中滑动?? **You need to use a Foreground Service to keep alive

当我打开应用程序时,startService会发送通知,但如果我关闭了我的应用程序,则永远不要停止应用程序并继续运行我的应用程序,就像手机短信一样,如果你关闭了短信应用程序但仍在运行

输出是当我打开应用程序并保持运行时,如果我的应用程序关闭,则服务会自动停止


我想继续运行我的应用程序

您是否尝试过使用Foreground服务?如果我的应用程序已关闭,请继续运行,或者在最近的应用程序中滑动??
**You need to use a Foreground Service to keep alive background service**

public class ForegroundService extends Service {
    public static final String CHANNEL_ID = "ForegroundServiceChannel";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

        //do heavy work on a background thread


        //stopSelf();

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

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

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }

}

------------------------------Start and Stop Forground Service----------------

public void startService() {
        Intent serviceIntent = new Intent(this, ForegroundService.class);
        serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android");

        ContextCompat.startForegroundService(this, serviceIntent);
    }

    public void stopService() {
        Intent serviceIntent = new Intent(this, ForegroundService.class);
        stopService(serviceIntent);
    }


 -----------------------------------Add Service in Manifest-------------------


 <service
            android:name=".ForegroundService"
            android:enabled="true"
            android:exported="true"></service>
**You need to use a Foreground Service to keep alive background service**

public class ForegroundService extends Service {
    public static final String CHANNEL_ID = "ForegroundServiceChannel";

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String input = intent.getStringExtra("inputExtra");
        createNotificationChannel();
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Foreground Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.ic_stat_name)
                .setContentIntent(pendingIntent)
                .build();

        startForeground(1, notification);

        //do heavy work on a background thread


        //stopSelf();

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

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

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }

}

------------------------------Start and Stop Forground Service----------------

public void startService() {
        Intent serviceIntent = new Intent(this, ForegroundService.class);
        serviceIntent.putExtra("inputExtra", "Foreground Service Example in Android");

        ContextCompat.startForegroundService(this, serviceIntent);
    }

    public void stopService() {
        Intent serviceIntent = new Intent(this, ForegroundService.class);
        stopService(serviceIntent);
    }


 -----------------------------------Add Service in Manifest-------------------


 <service
            android:name=".ForegroundService"
            android:enabled="true"
            android:exported="true"></service>