Sanket B
Sanket B

Reputation: 1119

Invoke service when screen turns on (Android)

I am creating a lockscreen app with facial recognition. As a first step I am creating a simple lockscreen that unlocks on Button click. The lockscreen should start when Switch on MainActivity is turned on. I created a Service which starts my lockscreen activity, but the activity does not show again once I turn off and then turn on my screen. I am a beginner in android and don't understand what to do. I would be happy if i could get some help.

The java files are:

MainActivity.java

package com.sanket.droidlockersimple;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;

public class MainActivity extends ActionBarActivity {

    private Switch enableLocker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        enableLocker = (Switch) findViewById(R.id.enableLocker);
        enableLocker.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(enableLocker.isChecked()) {
                    Intent intent = new Intent(MainActivity.this, DroidLockerService.class);
                    startService(intent);
                }
            }
        });
    }
}

DroidLockerService.java

package com.sanket.droidlockersimple;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class DroidLockerService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Intent intent1 = new Intent(this, LockScreen.class);
        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent1);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

LockScreen.java

package com.sanket.droidlockersimple;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class LockScreen extends ActionBarActivity {

    private Button unlock;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        setContentView(R.layout.activity_lock_screen);

        Button unlock = (Button) findViewById(R.id.button);
        unlock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

Upvotes: 2

Views: 113

Answers (1)

rothloup
rothloup

Reputation: 1290

I believe that what you need is a BroadcastReceiver

You have a service running, but something needs to tell the service that the screen has turned on or off. Android accomplishes this by issuing broadcast events whenever certain things occur (i.e. phone call received, system turned on, etc). Any registered broadcast receiver that has included the relevant BroadcastIntent will be informed that the event has occurred.

Your Broadcast Receiver can then tell your service that the event has occured and your service can respond appropriately.

I think that the particular broadcast events that you need to register for are "ACTION_SCREEN_ON" and "ACTION_SCREEN_OFF". See the Intent developer reference page for details.

Also read the developer guide for intents and intent filters in Android, and here is the BroadcastReceiver reference page. (StackOverflow won't let me post more than 2 links yet...just search for it on the Android developer website.)

Upvotes: 1

Related Questions