I am trying to make a Lock Screen App, that is why I want to start my app every time the Screen is turned on. Currently I found a solution where a Receiver for Screen on/off and the methods
and
are used. This is the Link of the Example: <a href="http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/#comment-4777" rel="nofollow">http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/#comment-4777</a>
I am using the example of the activity and not the service.
I am actually getting Feedback in the LogCat when the screen is turned off and on and the app is already running. The problem is that the app doesn't start when I am turning the screen on (even if it is shown under recently used apps).
I am not sure but I think the example works fine and I am just missing to add the essential code.
I hope someone can help !!
<hr>
Thanks for the quick answer. I have tried to run your code but it is still not working. I am not sure what is wrong. There is no error message. The application just runs normally but won't start when the screen goes on.
To make it easier to help me here is my code
This is my Receiver:
and this my Service:
}
Maybe you know what is wrong.
Code:
onPause()
Code:
onResume()
I am using the example of the activity and not the service.
I am actually getting Feedback in the LogCat when the screen is turned off and on and the app is already running. The problem is that the app doesn't start when I am turning the screen on (even if it is shown under recently used apps).
I am not sure but I think the example works fine and I am just missing to add the essential code.
I hope someone can help !!
<hr>
Thanks for the quick answer. I have tried to run your code but it is still not working. I am not sure what is wrong. There is no error message. The application just runs normally but won't start when the screen goes on.
To make it easier to help me here is my code
This is my Receiver:
Code:
package com.example.screenlocker;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
public static boolean screenOff = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = true;
Intent i = new Intent(context,LockService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = false;
}
}
and this my Service:
Code:
package com.example.screenlocker;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
public class LockService extends Service {
public void onCreate(){
IntentFilter filter=new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver locker=new StartMyServiceAtBootReceiver();
registerReceiver(locker, filter);
}
@Override
public void onStart(Intent intent, int startId) {
boolean screenOn = intent.getBooleanExtra("screen_state", false);
if(screenOn){
startActivity(new Intent(this, MainActivity.class));
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
Maybe you know what is wrong.