title: BroadcastReceiver for SCREEN_ON and SCREEN_OFF even after quitting the app
body: <p>I'm trying to make an app that monitors the users phone usage by tracking time of screen lock and unlock. I tried to setup a <code>BroadcastReceiver</code> which works fine when the app is running the background. But won't work when I close the app. Is there a solution for this.</p>
  
  <p>The code I'm using now is as follows :</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
  
         Intent intent = new Intent(this, ScreenListenerService.class);
         startService(intent);
     }
  
  }
  </code></pre>
  
  <p><code>ScreenListenerService</code> class is as follows..</p>
  
  <pre><code>public class ScreenListenerService extends Service {
  
     private BroadcastReceiver mScreenStateBroadcastReceiver = new BroadcastReceiver() {
  
         @Override
         public void onReceive(Context context, Intent intent) {
  
             if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
  
                 // Save something to the server
  
             } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
  
                 // Save something to the server
  
             }
  
         }
  
     };
  
     @Override
     public void onCreate() {
         super.onCreate();
         IntentFilter intentFilter = new IntentFilter();
         intentFilter.addAction(Intent.ACTION_SCREEN_ON);
         intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
         registerReceiver(mScreenStateBroadcastReceiver, intentFilter);
     }
  
     @Override
     public void onDestroy() {
         unregisterReceiver(mScreenStateBroadcastReceiver);
         super.onDestroy();
     }
  
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
         super.onStartCommand(intent, flags, startId);
         return START_STICKY;
     }
  
     @Override
     public IBinder onBind(Intent intent) {
         return null;
     }
  
  }
  </code></pre>
  
  <p>My <code>AndroidManifest</code> file is as follows :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.abbinvarghese.calculu"&gt;
  
     &lt;application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/AppTheme"&gt;
         &lt;service android:name=".ScreenListenerService" /&gt;
         &lt;activity android:name=".MainActivity"&gt;
             &lt;intent-filter&gt;
                 &lt;action android:name="android.intent.action.MAIN" /&gt;
                 &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
             &lt;/intent-filter&gt;
         &lt;/activity&gt;
     &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>