未显示通知并崩溃并提示 RemoteServiceException

Notification not being shown and crashes saying RemoteServiceException

我正在尝试在我的 android 中显示通知,服务如下所示:

public class VolumeService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        createNotification();
        return Service.START_STICKY;
    }
    void createNotification() {
        NotificationCompat.Builder builder
                = new NotificationCompat.Builder(this, "muavolume")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("Volume Service")
                .setContentText("Volume Service is Running")
                .setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_HIGH);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(0, builder.build());
    }
}

我只是简单地称呼它:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    void init(){
        Intent serviceIntent = new Intent(this,VolumeService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForegroundService(serviceIntent);
        }else{
            startService(serviceIntent);
        }
    }
}

清单更简单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mua.avs">
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AndroidVolumeService">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:enabled="true"
            android:name=".VolumeService"/>
    </application>
</manifest>

但是没有显示通知。大约 10 秒后,应用程序崩溃,显示:

2021-02-18 21:57:08.828 24254-24254/com.mua.avs E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mua.avs, PID: 24254
    android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{29af50c u0 com.mua.avs/.VolumeService}
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1975)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:225)
        at android.app.ActivityThread.main(ActivityThread.java:7564)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

我只想要一个永远的运行服务。我很困惑,因为这适用于另一个项目。我用的是miui-12

您应该像这样在您的服务中使用 startForeground

startForeground(NOTIFICATION_ID, notification);

而不是像这样在 activity 中使用它

startForegroundService(serviceIntent);

我不得不创建一个这样的通知渠道:

NotificationChannel channel = new NotificationChannel(getPackageName(),
                    "Volume Service",
                    NotificationManager.IMPORTANCE_HIGH);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            if (manager != null) {
                manager.createNotificationChannel(channel);
            }

这是full source