Saya sedang membangunkan program pengiklanan Bluetooth. Ia disusun dalam sdk 28. Seperti yang dikehendaki oleh dokumentasi, saya menambahkan kebenaran android.permission.foreground. Tetapi sekarang, jika saya mengklik butang "Iklan Bluetooth" dan memanggil startforeground(), apl itu ranap.
Selepas saya menambah kebenaran, tangkapan tidak pernah dicapai. Saya masih sampai ke urutan tangkapan sebelum menambah kebenaran, dan mesej memberitahu saya bahawa saya perlu menambah kebenaran foreground_service yang dinyatakan di atas.
Untuk aplikasi ini saya menggunakan java.
`/** * Starts BLE Advertising. */ private void startAdvertising() { goForeground(); Log.d(TAG, "Service: Starting Advertising"); if (mAdvertiseCallback == null) { AdvertiseSettings settings = buildAdvertiseSettings(); AdvertiseData data = buildAdvertiseData(); mAdvertiseCallback = new SampleAdvertiseCallback(); if (mBluetoothLeAdvertiser != null) { mBluetoothLeAdvertiser.startAdvertising(settings, data, mAdvertiseCallback); } } } /** * Move service to the foreground, to avoid execution limits on background processes. * * Callers should call stopForeground(true) when background work is complete. */ private void goForeground() { Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification n = new Notification.Builder(this) .setContentTitle("Advertising device via Bluetooth") .setContentText("This device is discoverable to others nearby.") .setSmallIcon(R.drawable.ic_launcher) .setContentIntent(pendingIntent) .build(); //TODO: startForeground throws Exception. Has to be Fixed try{ startForeground(FOREGROUND_NOTIFICATION_ID, n); } catch (SecurityException e) { Toast toast = Toast.makeText(this, e.getMessage() + " Exception Throwed", Toast.LENGTH_SHORT); toast.getView().setBackgroundColor(Color.parseColor("#FF0FF0")); toast.show(); } }`
Saya diminta untuk jejak tindanan, jadi inilah:
Pengecualian maut: Major Proses: com.example.android.bluetoothadvertisements, pid: 9760 android.app.remoteserviceexception$cannotpostforegroundservicenotificationexception: pemberitahuan ralat latar depan permulaan di android.app.activitythread.throwremoteserviceexception(activitythread.java:1983) di android.app.activitythread.-$$nest$mthrowremoteServiceException (Sumber tidak diketahui: 0) dalam android.app.activitythread$h.handlemessage(activitythread.java:2242) di android.os.handler.dispatchmessage(handler.java:106) di android.os.looper.looponce(looper.java:201) di android.os.looper.loop(looper.java:288) di android.app.activitythread.main(activitythread.java:7898) di java.lang.reflect.method.invoke(kaedah asli) di com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit.java:548) Dalam com.android.internal.os.zygoteinit.main(zygoteinit.java:936)
Kod anda sudah lapuk: gantikan kedua-dua baris
pendingintent pendingintent = ... notification n = ...
Oleh
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE); NotificationChannel notificationChannel = new NotificationChannel("your_Channel_ID", "your channel name", NotificationManager.IMPORTANCE_LOW); getSystemService(NotificationManager.class).createNotificationChannel(notificationChannel); Notification n = new Notification.Builder(this, "your_Channel_ID")
Atas ialah kandungan terperinci StartForeground_Service masih tidak berfungsi. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!