Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Call the <code>UpdateService.class</code> within your <code>MainActivity.class</code> . </p> <p><code>startService(new Intent(MainActivity.this, UpdateService.class));</code></p> <p><strong>UpdateService.class</strong></p> <pre><code>public class UpdateService extends Service { BroadcastReceiver mReceiver; public static int countOn = 0; public static int countOff = 0; @Override public void onCreate() { super.onCreate(); // register receiver that handles screen on and screen off logic Log.i("UpdateService", "Started"); IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_ANSWER); mReceiver = new MyReceiver(); registerReceiver(mReceiver, filter); } @Override public void onDestroy() { unregisterReceiver(mReceiver); Log.i("onDestroy Reciever", "Called"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub boolean screenOn = intent.getBooleanExtra("screen_state", false); if (!screenOn) { Log.i("screenON", "Called"); Log.i("viaService", "CountOn =" + countOn); Toast.makeText(getApplicationContext(), "Awake", Toast.LENGTH_LONG) .show(); } else { Log.i("screenOFF", "Called"); Log.i("viaService", "CountOff =" + countOff); } return START_STICKY; } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } } </code></pre> <p><strong>Receiver class</strong></p> <pre><code>public class MyReceiver extends BroadcastReceiver { private boolean screenOff; @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { screenOff = true; // Log.i("via Receiver","Normal ScreenOFF" ); } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { screenOff = false; } else if(intent.getAction().equals(Intent.ACTION_ANSWER)) { } Intent i = new Intent(context, UpdateService.class); i.putExtra("screen_state", screenOff); context.startService(i); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload