Prasad
Prasad

Reputation: 1375

Not able to unlock screen in android

I am able to lock a screen in android using Device policy manager. It will lock a screen even when the user is in other applications(Globaly). Same thing i want to do with Unlock. My application unlocks a screen when user locks a screen within my application. But when user locks a screen with other application it is not unlocking screen.

I am using proximity sensor to Lock/Unlock screen and registering ProximitySensorEventListener in onPause method for Unlock. It will unlock a screen only within my app.How to do a screen should unlock a screen with any other application (Globaly) ?? plz help me on this issue.

Thanks in advance.

Upvotes: 0

Views: 693

Answers (1)

Sathirar
Sathirar

Reputation: 331

Yes You have to implement a service running in your background along with the proximity sensor inputs for using it independent of your foreground application. Following code shows a service that runs in the background

public class PocketService extends Service implements SensorEventListener {

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

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_SHORT).show();
    Log.d("TAG", "onCreate");

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

    mSensorManager.registerListener(this, mProximity,
            SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_SHORT).show();
    Log.d("TAG", "onDestroy");
    mSensorManager.unregisterListener(this);
}

@Override
public int onStartCommand(Intent intent, int flags, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_SHORT).show();  
    Log.d("TAG", "onStart");
    return START_STICKY;
}

public void onSensorChanged(SensorEvent event) {
    Sensor sensor = event.sensor;
    if (sensor.getType() == Sensor.TYPE_PROXIMITY) {
        distance = event.values[0];
        if (distance < 1) {
                enableScreenLock();
        } else {
                disableScreenLock();
        }
         }

}
public void onAccuracyChanged(Sensor arg0, int arg1) {
    // TODO Auto-generated method stub

}
}

You have to start this from an activity and it will run in your background till the task manager kills it due to tight memory shortages. But it will try to recreate its instance while the memory is available because the onStartCommand() method return the START_STICKY.

Upvotes: 2

Related Questions