Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android 8前台服务系统通知“;“正在后台运行”;_Android_Android 8.0 Oreo_Foreground Service_Foregroundnotification - Fatal编程技术网

android 8前台服务系统通知“;“正在后台运行”;

android 8前台服务系统通知“;“正在后台运行”;,android,android-8.0-oreo,foreground-service,foregroundnotification,Android,Android 8.0 Oreo,Foreground Service,Foregroundnotification,我有面向安卓8的前台服务。当某些应用程序活动可见时,会出现类似“voip服务运行”的服务通知 问题是,当应用程序进入后台时,android会显示额外的系统通知“应用程序正在后台运行” 如何避免这种情况?用户不希望看到一个应用程序的两个正在进行的通知 , 代码: public class MyService extends Service { String TAG = "MyService"; public MyService() { } @Override

我有面向安卓8的前台服务。当某些应用程序活动可见时,会出现类似“voip服务运行”的服务通知

问题是,当应用程序进入后台时,android会显示额外的系统通知“应用程序正在后台运行”

如何避免这种情况?用户不希望看到一个应用程序的两个正在进行的通知

,

代码:

public class MyService extends Service {

    String TAG = "MyService";

    public MyService() {
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: ");

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            Notification notification = new NotificationCompat.Builder(this, TestApplication.PRIMARY_NOTIF_CHANNEL)
                    .setSmallIcon(android.R.drawable.ic_menu_call)
                    .setContentText("ServiceText")
                    .setContentTitle("ServiceTitle")
                    .setCategory(NotificationCompat.CATEGORY_SERVICE)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setOngoing(true)
                    .build();

            startForeground(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID, notification);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ");
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

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

public class TestApplication extends Application {
    public static final String PRIMARY_NOTIF_CHANNEL = "default";
    public static final int PRIMARY_FOREGROUND_NOTIF_SERVICE_ID = 1001;

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            NotificationChannel chan1 = new NotificationChannel(
                    PRIMARY_NOTIF_CHANNEL,
                    "default",
                    NotificationManager.IMPORTANCE_HIGH);

            chan1.setLightColor(Color.TRANSPARENT);
            chan1.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

            notificationManager.createNotificationChannel(chan1);
        }
    }
}


    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ContextCompat.startForegroundService(this, new Intent(this, MyService.class));
    }
}