我正在開發藍牙廣告程式。它在 sdk 28 中編譯。根據文件的要求,我新增了 android.permission.foreground 權限。但即使是現在,如果點擊「藍牙廣告」按鈕並呼叫 startforeground(),應用程式也會崩潰。
在我新增權限後,捕獲從未達到。在新增權限之前,我仍然會到達捕獲序列,並且訊息告訴我,我必須添加上面提到的 foreground_service 權限。
對於這個應用程序,我使用 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(); } }`
有人要求我提供堆疊跟踪,所以這就是:
致命異常:主要 進程:com.example.android.bluetoothadvertisements,pid:9760 android.app.remoteserviceexception$cannotpostforegroundservicenotificationexception:startforeground 的錯誤通知 在 android.app.activitythread.throwremoteserviceexception(activitythread.java:1983) 在 android.app.activitythread.-$$nest$mthrowremoteserviceexception(來源未知:0) 在 android.app.activitythread$h.handlemessage(activitythread.java:2242) 在 android.os.handler.dispatchmessage(handler.java:106) 在 android.os.looper.looponce(looper.java:201) 在 android.os.looper.loop(looper.java:288) 在 android.app.activitythread.main(activitythread.java:7898) 在 java.lang.reflect.method.invoke(本機方法) 在 com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit.java:548) 在com.android.internal.os.zygoteinit.main(zygoteinit.java:936)
正確答案
您的程式碼已過時:取代兩行
pendingintent pendingintent = ... notification n = ...
由
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")
以上是StartForeground_Service仍然不起作用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)