title: Android Calling Service from BroadcastReceiver
body: <p>I need to listen screen_on and screenof_action.</p>
  
  <p>I m using BroadcastReceiver with Service. But Im getting error. How can I fix my code? </p>
  
  <p><strong>this is my broadcastreceiver class :</strong> </p>
  
  <pre><code>public class BroadcastReceiver extends android.content.BroadcastReceiver {
      public static boolean screenOff = true;
      @Override
      public void onReceive(Context context, Intent intent) {
      if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
          screenOff = true;
         // System.out.println("System ON OLDU ON ON ON ON ON ON");
         Intent i = new Intent(context, MyNewService.class);
         i.putExtra("screen_state", true);
         context.startService(i);
      } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
          screenOff = false;
          Intent i = new Intent(context, MyNewService.class);
          i.putExtra("screen_state", false);
          context.startService(i);
         // System.out.println("System OFFFF OLDU OFFFF OFFFF OFFFF OFFFF");
      }
  }
  }
  </code></pre>
  
  <p><strong>this is my service class :</strong></p>
  
  <pre><code>public class MyNewService extends Service {
      BroadcastReceiver locker;
      public static boolean screenOn = true;
  
      public void onCreate(){
          IntentFilter filter = new IntentFilter();
          filter.addAction(Intent.ACTION_SCREEN_ON);
          filter.addAction(Intent.ACTION_SCREEN_OFF);
          locker=new BroadcastReceiver();
          registerReceiver(locker, filter);
      }
  
      @Override
      public void onStart(Intent intent, int startId) {
          Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
          Log.i("START", "onStart");
          screenOn = intent.getBooleanExtra("screen_state", false);
          if(screenOn){
             // startActivity(new Intent(this, MainActivity.class));
              Log.i("SCREEN", "SCREEN OFFFFFFFFFFFFFFFF");
          }   else {
              Log.i("SCREEN", "SCREEN OF FFFFFFFFFFFFFFFFFFFFFFFFF");
          }
      // If the screen is off then the device has been locked
      }
  
      @Override
      public void onDestroy(){
          unregisterReceiver(locker);
      }
  
      @Override
      public IBinder onBind(Intent arg0) {
          // TODO Auto-generated method stub
          return null;
      }
  }
  </code></pre>
  
  <p><strong>this is my error code :</strong> </p>
  
  <pre><code>    :19:52.696  32141-32141/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
         Process: com.codeexamples.app, PID: 32141
         java.lang.RuntimeException: Unable to start service  com.codeexamples.app.MyNewService@42672530 with Intent { cmp=com.codeexamples.app/.MyNewService }: java.lang.NullPointerException
              at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2705)
              at android.app.ActivityThread.access$2100(ActivityThread.java:135)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5001)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NullPointerException
  
       at com.codeexamples.app.MyNewService.onStart(MyNewService.java:25)
                at android.app.Service.onStartCommand(Service.java:450)
                at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2688)
                  at android.app.ActivityThread.access$2100(ActivityThread.java:135)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1293)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5001)
                  at java.lang.reflect.Method.invokeNative(Native Method)
  </code></pre>
  
  <p><strong>this is my manifest permisson :</strong></p>
  
  <p>for service :</p>
  
  <pre><code>    &lt;service
          android:name="com.codeexamples.app.MyNewService"
          android:enabled="true" /&gt;
  </code></pre>
  
  <p>for broadcast :</p>
  
  <pre><code>    &lt;receiver android:name="com.codeexamples.app.BroadcastReceiver" &gt;
      &lt;/receiver&gt;
  </code></pre>
  
  <p>for permission : </p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.DISABLE_KEYGUARD" /&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.BROADCAST_STICKY"/&gt;
  </code></pre>
  
  <p>When I m checking error in the logcat shows me this line :</p>
  
  <p>screenOn = intent.getBooleanExtra("screen_state", false);</p>
  
  <p><strong>Always show this line, I think something wrong in this line.</strong> </p>