Skip to main content

Detecting Screen on and Screen Off via services

Hi buddies, have a happy Sunday.

Today I'll explore about detecting screen on and screen off in android devices.

I got your mind, can use also by Broad cast Receivers. Yes, you can.

But how do you know, if app goes offline and screen off.

First write the Broad cast receiver class for receiving results.

ScreenReceiver.Java
--------------------------
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ScreenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
            Log.i("screenLog", "screen off");

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
            Log.i("screenLog", "screen on");
        }
    }
}

Then write the service class:

UpdateScreen.Java

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.appl.ex.receiver.ScreenReceiver;


public class UpdateScreen extends Service {
    BroadcastReceiver mReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
        // register receiver that handles screen on and screen off logic
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }


    @Override
    public void onDestroy() {
        unregisterReceiver(mReceiver);
        Log.i("onDestroy Reciever", "Called");
        super.onDestroy();
    }


    @Override
    public void onStart(Intent intent, int startId) {
        boolean screenOn = intent.getBooleanExtra("screen_state", false);
        if (!screenOn) {
            Log.i("screenON", "Called");
            Toast.makeText(getApplicationContext(), "Awake", Toast.LENGTH_LONG)
                    .show();
        } else {
            Log.i("screenOFF", "Called");
            Toast.makeText(getApplicationContext(), "Sleep",
                    Toast.LENGTH_LONG)
                    .show();
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }
}
Add the permission in Manifest, like this.

<uses-permission android:name="android.permission.DEVICE_POWER" />


Inside the Application:

<service android:name=".service.UpdateScreen" />

Finally start the service in your activity:

Intent service = new Intent(DrawerActivity.this, UpdateScreen.class);
startService(service);

Comments

Popular posts from this blog

Clear the app data programmatically in android

Clear the App Data Programmatically in Android

Application data has been created due to use shared preference data, databases and network caches data. This data has been manually clear on Settings -- > Apps (or) Application Manager --> Select the app you want to clear the data. --> Then click button clear data to erase the app from the Phone and SDCARD.

Applications like facebook, google+, gmail and some games captures more data on phone and SDCARD.

Once you clear the data of your app, all passwords and saved settings in app has been lost. So carefull to use this method.

Create the Class MyApplication

public class MyApplication extends Application {
 private static MyApplication instance;
 @Override
 public void onCreate() {
  super.onCreate();
  instance = this;
 }
 public static MyApplication getInstance(){
  return instance;
 }
 public void clearApplicationData() {
  File cache = getCacheDir();
  File appDir = new File(cache.getParent());
  if(appDir.exists()){
   String[] children = app…

convert an integer to a string in java

Hi, have a great day. Hope It's like to be homework question or to be interview question. Yes, it's right. In future posts, I'm gonna be post a interview questions and answers, algorithms and some critical problems.

Requirement:
1) return an empty string if value is negative.
2) return an empty string if the conversion fails.
3) return the proper string of the value using characer '0' to '9' and 'A' to 'F'
for example:
MyIntToString(254, 16) return "FE"
MyIntToString(254,  8) return "376"
MyIntToString(254,  2) return "11111110"


Rules:
1) You SHALL NOT import any Java libraries for the implementation.
2) You are allowed to using the string class related functions, like: insert(),
appened, trim()and etc for the implementation
3) You SHALL NOT invoke static functions from java.lang.Integer class to perform the conversion.
4) Your code style is graded, hence it is encouraged to structure the code professionally
and equip…

Sort the numbers using If, Else and Loop.

This is very interesting. You need to sort the numbers, but not using the SORT functions. You can use the If, Else or Loop to do the functions.

See the solution:

import java.util.Scanner;
public class OrderNumbers {
    public static void main(String[] args) {         Scanner in = new Scanner(System.in);         System.out.println("Enter the size of an Array");         int num = in.nextInt();         int [] numbers = new int[num];         for (int i = 0; i < numbers.length; i++) {             System.out.println("Enter the number "+(i+1)+" : ");             numbers[i] = in.nextInt();         }         sortNumbers(numbers);     }     static void sortNumbers(int[] numbers){         for (int i = 0; i < numbers.length; i++) {             int newValue;             for (int j = 0; j < numbers.length; j++) {                 if (numbers[i] < numbers[j]) {                     newValue = numbers[j];                     numbers[j] = numbers[i];                     numbers[i]…