Problem registering a BroadcastReceiver in extended Service

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Problem registering a BroadcastReceiver in extended Service

Postby FIN_Master » Sun Dec 12, 2010 12:37 pm

I have this class which extends Service and needs a static BroadcastReceiver. Now when I make an instance of this class from another service, it fails registering the receiver throwing:

12-12 13:27:16.369: ERROR/AndroidRuntime(300): Caused by: java.lang.NullPointerException
12-12 13:27:16.369: ERROR/AndroidRuntime(300): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
12-12 13:27:16.369: ERROR/AndroidRuntime(300): at conditions.BatteryCondition.<init>(BatteryCondition.java:46)


My code is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public final class BatteryCondition extends Condition {
  2.         protected static someValue;
  3.         private static boolean registered = false;
  4.  
  5.         private static BroadcastReceiver listener = new BroadcastReceiver() {
  6.                 if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
  7.                         someValue = intent.getIntExtra("level", 0);
  8.                 }
  9.         }
  10.  
  11.         public BatteryCondition( )
  12.         {
  13.                 super( ConditionType.BATTERY );
  14.  
  15.                 if( !registered )
  16.                 {              
  17.                         registerReceiver(BatteryCondition.listener, new IntentFilter());
  18.                         registered = true;
  19.                 }
  20.         }
  21.  
  22.         some other code;
  23. }
  24.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I tried also registering the receiver in manifest but this way i get no intents to receiver.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <receiver android:name=".conditions.BatteryCondition" android:enabled="true">
  2.         <intent-filter>
  3.                 <action android:name="Intent.ACTION_BATTERY_CHANGED" />
  4.         </intent-filter>
  5. </receiver>
  6.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
FIN_Master
Freshman
Freshman
 
Posts: 2
Joined: Sun Dec 12, 2010 12:01 pm

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: Alexa [Bot] and 8 guests