0

I want to handle an Androids application work in the background. I've read that I have to use services for this. In this application, I need to know when the screen gets locked and when it gets unlocked again. Every time this happens, I have to do some actions.

For activities, I've seen we have onPause and onRestart for this, but I need to know when the screen is turned off and when it is turned on in a service. How can this information be retrieved from inside a service.

BDL
  • 21,052
  • 22
  • 49
  • 55
Simone
  • 29
  • 1
  • 7
  • Please make sure that you really, really want to know whether the devices is _locked_, because as you can see from the "discussion" regarding the potential duplicate question, this may never be the case. Also, I think that the method suggested there is not the way it should be done, but that's another point. – class stacker Jun 06 '17 at 15:54

4 Answers4

1

Turning screen on/off produces system events. You must listen to those broadcasts and capture them. Add this to your manifest:

<receiver android:name=".ScreenLockBroadcastReceiver">
  <intent-filter>
    <action android:name="android.intent.action.USER_PRESENT" />
    <action android:name="android.intent.action.ACTION_SHUTDOWN" />
 </intent-filter>
</receiver>

And then register a broadcast receiver to capture it inside your service.

public class ScreenLockBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
            // do stuff
        }
    }
}
Robert K.
  • 1,043
  • 1
  • 8
  • 20
  • I've try now , and this work ! But this code only tell me when screen is on , and not when screen is locked. I have prove with add this at the code: if(intent.getAction().equals(Intent.ACTION_SHUTDOWN)) But not work , you know how i can add this funcition? – Simone Jun 06 '17 at 12:59
  • You need to check a list of system broadcasts and find the ones you need exactly. You can find that list over here: https://developer.android.com/reference/android/content/Intent.html – Robert K. Jun 06 '17 at 13:09
  • Also, in case only the first intent is caught, try putting them in separate ``s in your manifest – Robert K. Jun 06 '17 at 13:09
0

You can use this

KeyguardManager keyguardManager = (KeyguardManager) 
context.getSystemService(Context.KEYGUARD_SERVICE);
if( keyguardManager.inKeyguardRestrictedInputMode()) {
  //it is locked
  } else {
  //it is not locked
}

Hope this helps.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
  • Hi thanks for reply , but where i've to add this ? i want the service ever work in background , And it must always capture the screen ON and Screen Off . i have try to insert this code in the constructor of service but i have an error : java.lang.RuntimeException: Unable to instantiate service com.example.simon.dataconnection.DisableDataService: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference – Simone Jun 06 '17 at 13:34
  • Regarding thr service i will have to research about this. – Sarthak Gandhi Jun 06 '17 at 13:41
  • It looks like you copied it from [here](https://stackoverflow.com/a/8668648/1856738). 1) This is the exact Q&A to which this Q is linked as a duplicate. 2) The original A mentions a restriction which you did not copy. Please take into account that copying on a Q&A website is not helpful. The fewer copies, the higher the chances to find the right answers -- simple search robot arithmetics. Yes it's more difficult to accumulate credit points in this case, but it leads to higher quality. – class stacker Jun 07 '17 at 13:09
  • @Simone Obviously, the above recommendation is not what Android wants you to do -- asking a keyboard status for finding out the system state is really bad practice, and it will certainly stop working in some future release. Also, there are allternatives to doing things in Service constructors -- even in official online Android documentation. Why don't you search a bit and ask a new question, providing us with more background as I already suggested. – class stacker Jun 07 '17 at 13:13
0

Use this actions in receiver tag in manifest

 <action android:name="android.intent.action.SCREEN_ON" />
 <action android:name="android.intent.action.SCREEN_OFF" />
Pratik
  • 452
  • 4
  • 17
  • I have to try with this , i've insert this in manifest , and in the receiver in method onReceive i've insert this two method , if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)) and if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) But not work , when my screen is on or off not enter in this if. You know how i can solve this ? – Simone Jun 06 '17 at 13:18
  • Have you registered your broadcast? – Pratik Jun 06 '17 at 15:14
  • ScreenLockBroadcastReceiver mBroadcastReceiver = new ScreenLockBroadcastReceiver(); IntentFilter screenStateFilter = new IntentFilter(); screenStateFilter.addAction(Intent.ACTION_SCREEN_ON); screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF); registerReceiver(mBroadcastReceiver, screenStateFilter); – Pratik Jun 06 '17 at 15:15
0

I want create android application work in background , I've read that I have to use the services.

Yes, probably you have to use services to check is screen still on or went off. For example in onCreate of your Services class.

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); BroadcastReceiver broadcast = new MyBroadcastReciverClass(); //or registerReceiver(broadcast , intentFilter );

Actually:

Intent.ACTION_SCREEN_OFF 

or

Intent.ACTION_SCREEN_ON

Are 2 main things that will have to go in your BroadcastReceiver too. For example in your onReceive method:

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        // CODE HERE FOR SCREEN OFF
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        // CODE HERE FOR SCREEN ON
    }

For more detailed expnlanation take a look in this article: https://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

Yupi
  • 4,402
  • 3
  • 18
  • 37