Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
All valid answers to this subject has either been outdated or answered in 2012/2013. I followed particulary this answer: android: broadcast receiver for screen on and screen off

Unfortunately the answer didn't came with an example. So after 20 hours of research and programming I give up. All I need is to detect if the lock key is clicked three times with my broadcast so I can activate the services. I do not want an activity which will no longer work after the user shut it down. This service has to run at all time. Right now the app is crashing.

What I have tried:

C#
public class ScreenReceiver extends BroadcastReceiver {

private boolean screenOff;

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        screenOff = true;
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        screenOff = false;
    }
    Intent i = new Intent(context, BackgroundService.class);
    i.putExtra("screen_state", screenOff);
    context.startService(i);
}

public class BackgroundService extends Service{

private boolean isRunning;
private Context context;
private Thread backgroundThrad;

private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean screenOn = intent.getBooleanExtra("screen_state",false);
        if(!screenOn){
            System.out.println("SCREEN OFF");
        }
        if(screenOn){
            System.out.println("CREEN ON");
        }
    }
};

@Override
public void onCreate() {
    this.context = this;
    this.isRunning = false;
    this.backgroundThrad = new Thread(myTask);

    // REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
    IntentFilter screenStateFilter = new IntentFilter();
    screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
    screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
    registerReceiver(mScreenStateReceiver, screenStateFilter);
}

@Override
public void onDestroy() {
    unregisterReceiver(mScreenStateReceiver);
    this.isRunning = false;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    if(!this.isRunning){
        this.isRunning = true;
        this.backgroundThrad.start();
    }
    return START_STICKY;
}

private Runnable myTask = new Runnable() {
    @Override
    public void run() {
        System.out.println("THE BACKGROUND SERVICE IS RUNNING");
        stopSelf();
    }
};

}

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <service android:name=".BackgroundService"> </service>
</application>
Posted
Updated 23-Oct-16 0:55am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900