As a first aproximation to my final application, the code I've implemented (shown below) activate changes between disabling and enabling screen lock.
For this, I use:
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.reenableKeyguard();
lock.disableKeyguard();
When I pass the KeyguardLock object to the service AplicacionContextos, the disable and reenable of the screen lock doesn't work. I don't know the reason why this is happening. When I don't use the service and disable and re enable the Keyguard in the MainActivity it works fine, but not in the service.
Could anyone help me to fix my program?
If not,anyone knows how to implement disable and re enable of screen locks in background, I mean, still working when the user leaves the application?
Thanks!
This is my code:
public class MainActivity extends Activity { private static PendingIntent pendingIntent;
KeyguardManager keyguardManager;
KeyguardLock lock;
Button m_btnAlarma = null;
BBDDHandler manejador = new BBDDHandler();
String tipoSeguridadActual;
AplicacionContextos ac = new AplicacionContextos();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_btnAlarma = ((Button)findViewById(R.id.button2));
m_btnAlarma.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
//cambiarEstadoBloqueo(lock);
lanzarCambio();
}
});
}
private void lanzarCambio(){
if (pendingIntent == null){
//La alarma está desactivada, la activamos
activarContexto();
}else{
//La alarma está activada, la desactivamos
desactivarContexto();
}
}
/** desactivarRecibir()
* Método que desactiva el uso de contextos
*/
private void desactivarContexto(){
keyguardManager = null;
lock = null;
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
m_btnAlarma.setText("OFF");
pendingIntent = null;
// Se cambia la seguridad a alta !!!!!!!!! ESTABLECER
System.out.println("Seguridad del dispositivo ALTA al desactivarse el uso de contextos");
Toast.makeText(this, "Hilo detenido", Toast.LENGTH_LONG).show();
}
/** activarContexto()
* Método que activa el uso de contextos
*/
private void activarContexto(){
keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
ac.setLock(lock);
int comprobacionIntervaloSegundos = 60; // 1 minuto
Intent myIntent = new Intent(this, AplicacionContextos.class);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), comprobacionIntervaloSegundos * 1000, pendingIntent);
m_btnAlarma.setText("ON");
Toast.makeText(this, "Uso iniciado", Toast.LENGTH_LONG).show();
}
}
public class AplicacionContextos extends Service implements Runnable {
NotificationManager mManager;
BBDDHandler manejador = new BBDDHandler();
String tipoSeguridadActual;
private KeyguardLock lock;
/** run()
* Hilo de ejecución
*/
public void run() {
try{
cambiarEstadoBloqueo(); // se modifica la seguridad cada minuto
}
catch(Exception e){
}
}
public void setLock(KeyguardLock l){
System.out.println("Se asigna la variable lock");
lock = l;
}
public void cambiarEstadoBloqueo (){
tipoSeguridadActual = manejador.obtenerTipoActual(this);
if ( tipoSeguridadActual == null){
System.out.println("Alta");
manejador.almacenarTipoActual(this, "Alta");
}
else{
// si el bloqueo esta desactivado se activa
if(tipoSeguridadActual.equals("Baja")){ // Baja
System.out.println("Alta");
manejador.modificarSeguridadActual(this, "Alta");
lock.reenableKeyguard();
// si el bloqueo está activado se desactiva
}else{ // Alta
manejador.modificarSeguridadActual(this, "Baja");
lock.disableKeyguard();
}
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
// Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
// Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
// Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
//Creamos un hilo que obtendra la informaci—n de forma as’ncrona
Thread thread = new Thread(this);
thread.start();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
// Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
}