Inhaltsverzeichnis
Richtige Antwort
Heim Java StartForeground_Service funktioniert immer noch nicht

StartForeground_Service funktioniert immer noch nicht

Feb 08, 2024 pm 08:42 PM

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();
    }

}`
Nach dem Login kopieren

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 = ...
Nach dem Login kopieren

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")
Nach dem Login kopieren

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!

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat -Befehle und wie man sie benutzt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)