2017-05-16 2 views
0

Ich möchte eine Broadcast und mein Receiver sollte die Übertragung empfangen, aber es funktioniert nicht.
Ich habe den folgenden Code:Broadcast-Empfänger nicht empfangen

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="utilities.dip.com.checkbattlevelstackof"> 

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <service 
     android:name=".YourService" 
     android:enabled="true" 
     android:exported="false" 
     android:label="@string/app_name" > 
    </service> 

    <!-- Receivers --> 
    <receiver 
     android:name=".AlarmReceiver" 
     android:enabled="true" /> 
    <receiver 
     android:name=".BootReceiver" 
     android:enabled="true" 
     android:exported="true" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

Der Empfänger ist:

package utilities.dip.com.checkbattlevelstackof; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 



public class BootReceiver extends BroadcastReceiver { 

    public static final String TAG = "BootReceiver"; 
    public static final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equalsIgnoreCase(ACTION_BOOT)) { 
      // This intent action can only be set by the Android system after a boot 
      Log.d(TAG,"Received boot event"); 
      Intent monitorIntent = new Intent(context, YourService.class); 
      monitorIntent.putExtra(YourService.HANDLE_REBOOT, true); 
      context.startService(monitorIntent); 
     } 
     else{ 
      Log.d(TAG," Action received : " + intent.getAction()); 
     } 
    } 
} 

Wenn ich eine Sendung ich jedes Protokoll bin nicht mache immer:

platform-tools $ ./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.DEFAULT -n utilities.dip.com.checkbattlevelstackof/utilities.dip.com.checkbattlevelstackof.BootReceiver 
Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED cat=[android.intent.category.DEFAULT] cmp=utilities.dip.com.checkbattlevelstackof/.BootReceiver } 
Broadcast completed: result=0 

Was der Fehler ist ?

+0

Entfernen Sie die if-Anweisung. Wenn Sie einen bestimmten Intent-Filter in Manifest setzen, müssen Sie ihn nicht erneut in der Klasse überprüfen. Außerdem müssen Sie keine Kategorie für den Empfänger hinzufügen. –

+0

Fertig, nichts ist passiert. Siehe ich habe auch noch eine andere, nur um zu überprüfen. Kein Protokoll erstellt – Dip

+0

Wie wäre es mit einer Anfrage? (wenn Sie auf api> 22 sind) –

Antwort

1

Das liegt daran, dass Sie in Ihrem Manifest außerhalb deklariert haben. Es sollte grundsätzlich so aussehen:

<application 
     ..... 
    <receiver 
     android:name=".BootReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
</application> 

Auch beim Überprüfen der Aktion Zeichenfolge für die Standard-Android-Aktionen, verwenden Sie nicht Ihre eigene Konstante. Anstatt die ACTION_BOOT-Konstante zu verwenden, verwenden Sie "Intent.ACTION_BOOT_COMPLETED"

+1

danke, funktioniert – Dip

1

Empfänger hinzufügen & Service innerhalb des Anwendungs-Tags.