Saya menggunakan Layanan Foreground untuk notifikasi dan berfungsi di ponsel lain juga berfungsi pada emulator juga tetapi di ponsel MI tidak berfungsi, ini menunjukkan kesalahan seperti ini.

2020-07-01 17:47:25.783 4338-4338/com.example.notification_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.notification_app, PID: 4338
java.lang.RuntimeException: Unable to create service com.example.notification_app.my_service: java.lang.SecurityException: Permission Denial: startForeground from pid=4338, uid=10175 requires android.permission.FOREGROUND_SERVICE
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3577)
    at android.app.ActivityThread.access$1400(ActivityThread.java:200)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1689)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6810)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
 Caused by: java.lang.SecurityException: Permission Denial: startForeground from pid=4338, uid=10175 requires android.permission.FOREGROUND_SERVICE
    at android.os.Parcel.createException(Parcel.java:1953)
    at android.os.Parcel.readException(Parcel.java:1921)
    at android.os.Parcel.readException(Parcel.java:1871)
    at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5104)
    at android.app.Service.startForeground(Service.java:695)
    at com.example.notification_app.my_service.startMyOwnForeground(my_service.java:57)
    at com.example.notification_app.my_service.onCreate(my_service.java:44)
    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3565)
    at android.app.ActivityThread.access$1400(ActivityThread.java:200) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1689) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:201) 
    at android.app.ActivityThread.main(ActivityThread.java:6810) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873) 

01-07-2020 17:47:25.826 4338-4338/? I/Proses: Mengirim sinyal. PID: 4338 SIG: 9

Dan di baris nomor 48, saya memanggil fungsi startForeground(1, new Notification());

0
Sejpalsinh Jadeja 1 Juli 2020, 15:20

1 menjawab

Jawaban Terbaik

Jika Anda membaca dengan seksama di akhir baris kedua

requires android.permission.FOREGROUND_SERVICE

Saya juga menghadapi kesalahan yang sama di salah satu proyek saya, saya menyelesaikannya dengan menambahkan izin di file manifes. Jadi cukup tambahkan izin latar depan di file manifes.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"></uses-permission>

Semoga berhasil untuk Anda.

2
user10168551user10168551 1 Juli 2020, 12:28