Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
Java 前台服务的通知不';t在安卓8上显示;_Java_Android_Android Notifications_Android 8.0 Oreo_Foreground Service - Fatal编程技术网

Java 前台服务的通知不';t在安卓8上显示;

Java 前台服务的通知不';t在安卓8上显示;,java,android,android-notifications,android-8.0-oreo,foreground-service,Java,Android,Android Notifications,Android 8.0 Oreo,Foreground Service,我的前台服务在Android Oreo上运行时不会显示通知 它可以在15到25个Android版本上完美运行。 当我执行targetSdkVersion从26到25时,此问题消失。但这个解决方案似乎并不好 我准备好了这个问题 在Android Oreo上用targetSdkVersion 26修复它,我应该怎么做? 我的前台服务SoundService.java(): 我的身材。格雷德尔: apply plugin: 'com.android.application' repositories

我的前台服务在Android Oreo上运行时不会显示通知

它可以在15到25个Android版本上完美运行。 当我执行
targetSdkVersion
26
25
时,此问题消失。但这个解决方案似乎并不好

我准备好了这个问题

在Android Oreo上用
targetSdkVersion 26
修复它,我应该怎么做?

我的前台服务SoundService.java():

我的身材。格雷德尔:

apply plugin: 'com.android.application'

repositories {
    maven { url 'https://maven.google.com' }
}

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.foreground"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "com.android.support:support-compat:${project.ext.supportLibVersion}"
    compile "com.android.support:support-v4:${project.ext.supportLibVersion}"
    compile "com.android.support:design:${project.ext.supportLibVersion}"
    compile "com.android.support:appcompat-v7:${project.ext.supportLibVersion}"
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}
其中
project.ext.supportLibVersion='26.1.0'

查看它如何在Android API上工作第1步:将
project.ext.supportLibVersion
设置为
26.1.0
或更高版本

步骤2:请注意,您现在在所有
new NotificationCompat.Builder()
调用中都收到了弃用警告

步骤#3:(如果您在应用程序的前一次运行中没有定义它)


步骤4:将频道ID传递给
NotificationCompat.Builder
constructor

我刚刚在
SoundService.java
中更改了我的方法
prepareNotification()

    private Notification prepareNotification() {
            Intent notificationIntent = new Intent(this, MainActivity.class);
            notificationIntent.setAction(MusicConstants.ACTION.MAIN_ACTION);
            //...
            NotificationCompat.Builder lNotificationBuilder = new NotificationCompat.Builder(this);
           //...
致:


您可以看到完全不同。

尽管创建了前台服务,但定义
通知频道不会使该服务成为Oreo中操作系统要杀死的候选服务吗?我确实注意到前台服务在运行了一段时间后被杀死了@ranjjose:“如果我不接受通知频道,请让该服务成为Oreo操作系统杀死的候选者?”--我没有尝试过这种情况,对不起。感谢您的快速响应。顺便说一句,我修正了一些拼写错误。
    private Notification prepareNotification() {
            Intent notificationIntent = new Intent(this, MainActivity.class);
            notificationIntent.setAction(MusicConstants.ACTION.MAIN_ACTION);
            //...
            NotificationCompat.Builder lNotificationBuilder = new NotificationCompat.Builder(this);
           //...
    private Notification prepareNotification() {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O &&
                    mNotificationManager.getNotificationChannel(FOREGROUND_CHANNEL_ID) == null) {
                // The user-visible name of the channel.
                CharSequence name = getString(R.string.text_value_radio_notification);
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel mChannel = new NotificationChannel(FOREGROUND_CHANNEL_ID, name, importance);
                mChannel.enableVibration(false);
                mNotificationManager.createNotificationChannel(mChannel);
            }
            Intent notificationIntent = new Intent(this, MainActivity.class);
            notificationIntent.setAction(MusicConstants.ACTION.MAIN_ACTION);
           //...
           NotificationCompat.Builder lNotificationBuilder;
           if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
           lNotificationBuilder = new NotificationCompat.Builder(this, FOREGROUND_CHANNEL_ID);
           } else {
           lNotificationBuilder = new NotificationCompat.Builder(this);
           }
           //...