屏幕锁在背景中

时间:2013-08-24 23:49:20

标签: android service background screen-lock

作为我最终应用程序的第一个aproximation,我实现的代码(如下所示)激活了禁用和启用屏幕锁定之间的更改。

为此,我使用:

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);

KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);  

lock.reenableKeyguard();        
lock.disableKeyguard();

当我将KeyguardLock对象传递给服务AplicacionContextos时,屏幕锁定的禁用和重新启用不起作用。我不知道为什么会这样。当我不使用该服务并禁用并重新启用MainActivity中的Keyguard时,它可以正常工作,但不在服务中。

有人可以帮我修理我的程序吗?

如果没有,任何人都知道如何在后台实现禁用和重新启用屏幕锁定,我的意思是,当用户离开应用程序时仍然可以正常工作?

谢谢!

这是我的代码:

公共类MainActivity扩展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);
}

}

0 个答案:

没有答案