簡體   English   中英

未顯示通知並崩潰說 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);

而不是像這樣在您的活動中使用它

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);
            }

這是完整的來源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM