目錄
正確答案
首頁 Java StartForeground_Service仍然不起作用

StartForeground_Service仍然不起作用

Feb 08, 2024 pm 08:42 PM

問題內容

我正在開發藍牙廣告程式。它在 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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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