为什么我的前台服务停止,如果我按下按钮“清除所有”

iq3niunx  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(464)

我在舱单上的许可
使用权限android:name=“android.permission.u服务”
服务是
android:stopwithtask=“错误”
onstartcommand返回start\u并创建此代码的通知
私有void startforegroundservice(){

Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent =
            PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Notification notification = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        notification = new Notification.Builder(this, "SEX")
                        .setContentTitle("getText(R.string.notification_title)")
                        .setContentText("getText(R.string.notification_message)")
                        .setSmallIcon(R.drawable.ic_launcher_background)
                        .setContentIntent(pendingIntent)
                        .setTicker("ssss")
                        .build();
    }else {
        notification = new Notification.Builder(this)
                .setContentTitle("getText(R.string.notification_title)")
                .setContentText("getText(R.string.notification_message)")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentIntent(pendingIntent)
                .setTicker("ssss")
                .build();
    }
    startForeground(423, notification);

}

但如果我按“全部清除”按钮,我的服务就没用了

7lrncoxx

7lrncoxx1#

你需要的是一个持续的通知,把它放在你的电脑上 Notification.Builder 对象:

notification = ...
        ...
        .setOngoing(true)
        .build();

相关问题