Heim  >  Artikel  >  StartForeground_Service funktioniert immer noch nicht

StartForeground_Service funktioniert immer noch nicht

WBOY
WBOYnach vorne
2024-02-08 20:42:23379Durchsuche
Frageninhalt

Ich entwickle ein Bluetooth-Werbeprogramm. Es ist in SDK 28 kompiliert. Wie in der Dokumentation gefordert, habe ich die Berechtigung android.permission.foreground hinzugefügt. Aber selbst jetzt stürzt die App ab, wenn ich auf die Schaltfläche „Bluetooth-Werbung“ klicke und startforeground() aufrufe.

Nachdem ich die Berechtigungen hinzugefügt hatte, wurde die Erfassung nie erreicht. Ich komme immer noch zur Aufnahmesequenz, bevor ich die Berechtigungen hinzufüge, und die Meldung sagt mir, dass ich die oben erwähnten foreground_service-Berechtigungen hinzufügen muss.

Für diese Anwendung verwende ich 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();
    }

}`

Ich wurde nach einem Stacktrace gefragt, also hier ist es:

Fatale Ausnahme: Major Prozess: com.example.android.bluetoothadvertisements, PID: 9760 android.app.remoteserviceException$cannotpostforegroundservicenotificationException: Fehlerbenachrichtigung von Startforeground bei android.app.activitythread.throwremoteserviceException(activitythread.java:1983) bei android.app.activitythread.-$$nest$mthrowremoteServiceException (Unbekannte Quelle: 0) in android.app.activitythread$h.handlemessage(activitythread.java:2242) unter android.os.handler.dispatchmessage(handler.java:106) unter android.os.looper.looponce(looper.java:201) unter android.os.looper.loop(looper.java:288) unter android.app.activitythread.main(activitythread.java:7898) bei java.lang.reflect.method.invoke(native Methode) unter com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit.java:548) In com.android.internal.os.zygoteinit.main(zygoteinit.java:936)


Richtige Antwort


Ihr Code ist veraltet: Ersetzen Sie beide Zeilen

pendingintent pendingintent = ...
notification n = ...

Von

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")

Das obige ist der detaillierte Inhalt vonStartForeground_Service funktioniert immer noch nicht. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Dieser Artikel ist reproduziert unter:stackoverflow.com. Bei Verstößen wenden Sie sich bitte an admin@php.cn löschen