首頁  >  文章  >  StartForeground_Service仍然不起作用

StartForeground_Service仍然不起作用

WBOY
WBOY轉載
2024-02-08 20:42:23453瀏覽
問題內容

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

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除