Device not locked after WakeLock

131 views Asked by At

I have some problem with a code which is called by AlarmManager. I call a wakeLock to do a task in order to transfer data to a web services My wakelock is quite well executed and after my upload, i call the method release But after that, my device doesn't lock anymore with the power button and I don't understand why .... (in order to explain, the device screen is off but the lock screen doesn't appear anymore when i press again the power button, it is unlocked) If someone could help me

my code :

package com.onyx.telegestion;

import java.util.Timer;
import java.util.TimerTask;

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.app.admin.DevicePolicyManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.ResultReceiver;
import android.os.SystemClock;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {

    private NotificationManager mManager;

    private String alarmServiceTag = "ALARMRECEIVERTAG";

    private boolean transfertSuccess;

    private boolean planningSuccess;

    private boolean ficheTacheSuccess;

    private boolean execute;

    private String message = "";

    private Context context;

    private WakeLock wakeLock;

    private NetworkReceiver receiver = new NetworkReceiver();

    private Handler handlerTG;

    private Handler handlerPlanning;

    private Handler handlerFicheTache;

    private TimerTask tTask;

    private Timer timer;

    private PowerManager pm;



    //the amount of time after which you want to stop the service
    private final long INTERVAL = 1000 * 60 * 5; // en ms


    @Override
    public void onReceive(Context context, Intent intent) {
        //wakelock acquire
        pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), alarmServiceTag);       
        wakeLock.acquire();

            execute = false;
            transfertSuccess = false;
            planningSuccess = false;
            ficheTacheSuccess = false;

            handlerTG = new Handler();
            handlerPlanning = new Handler();
            handlerFicheTache = new Handler();

            this.context = context;

            final Context con = context.getApplicationContext();



            tTask = new TimerTask()
            {
                public void run()
                {
                    try{
                        if(!execute){
                            //on supprime le receiver
                            Log.i(alarmServiceTag, "Arret du transfert auto : TIMEOUT");
                            con.unregisterReceiver(receiver);
                            try{
                                wakeLock.release();
                                Log.i(alarmServiceTag, "release du wakelock");

                            }catch(Exception e){
                                Log.e(alarmServiceTag, e.toString());
                            }
                        }
                    }catch(Exception e){
                        Log.e(alarmServiceTag, e.toString());
                    }
                }
            };

            timer = new Timer();



            /*if(!execute){
                execute = true;
                callTransfert();
            }*/

            // on enregistre le networkReceiver
            IntentFilter filter = new IntentFilter(
                    ConnectivityManager.CONNECTIVITY_ACTION);
            receiver = new NetworkReceiver();
            context.getApplicationContext().registerReceiver(receiver, filter);


    }



        public void notifyService() {
            mManager = (NotificationManager) context.getSystemService(
                    context.NOTIFICATION_SERVICE);
            Intent intent1 = new Intent(context,
                    MainActivity.class);

            Notification notification = new Notification(R.drawable.ic_launcher,
                    message, System.currentTimeMillis());
            intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);

            PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
                    context, 0, intent1,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.setLatestEventInfo(context,
                    "Transfert Automatique", message, pendingNotificationIntent);

            mManager.notify(0, notification);

            //on supprime le receiver
            context.getApplicationContext().unregisterReceiver(receiver);

            /*KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); 
            KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock(alarmServiceTag);
            keyguardLock.disableKeyguard();*/
            try{
                wakeLock.release();
                Log.i(alarmServiceTag, "release du wakelock");

            }catch(Exception e){
                Log.e(alarmServiceTag, e.toString());
            }
        }


        public void callTransfert() {

            /**
             * Partie pour le transfert de la TG
             */
            execute = true;
            Intent intent = new Intent(context, TransferService.class);
            intent.putExtra("receiver", new TransferReceiver(handlerTG));
            // handlerMAJ.removeCallbacks(runableMAJ);
            context.startService(intent);

            /**
             * Partie pour le transfert du planning
             */

            /**
             * Partie pour le transfert des fiches de tâches
             */

        }

        private class TransferReceiver extends ResultReceiver {
            public TransferReceiver(Handler handler) {
                super(handler);
            }

            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                super.onReceiveResult(resultCode, resultData);

                if (resultCode == TransferService.UPDATE_PROGRESS) {
                    int progress = resultData.getInt("progress");
                    Log.i(alarmServiceTag, "Pourcentage progression : " + progress);

                } else if (resultCode == TransferService.TRANSFERT_FAILED) {

                    System.out.println("dans le transfert : FAILED");

                    message = "Il y a eu une(ou plusieurs) erreur(s) durant la transmission des interventions";
                    transfertSuccess = false;

                    Log.i(alarmServiceTag, "Erreur durant le transfert de la TG");

                    execute = false;
                    notifyService();

                } else if (resultCode == TransferService.TRANSFERT_SUCCESS) {

                    System.out.println("dans le transfert : SUCCESS");
                    Log.i(alarmServiceTag, "Succès lors du transfert de la TG");
                    transfertSuccess = true;

                    Log.i(alarmServiceTag, "Début du transfert du planning");

                    /**
                     * Partie pour le transfert du planning
                     */

                    // appel du service
                    Intent intent = new Intent(context,
                            PlanningService.class);
                    intent.putExtra("receiver", new PlanningReceiver(handlerPlanning));
                    context.startService(intent);

                }
            }
        }

        /**
         * Classe qui permet de gérer un ResultReceiver personnalisé pour déclencher
         * les actions personnalisées lors du Transfert du planning
         * 
         * @author Anthony Fischer
         * 
         */
        private class PlanningReceiver extends ResultReceiver {
            public PlanningReceiver(Handler handler) {
                super(handler);
            }

            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                super.onReceiveResult(resultCode, resultData);

                if (resultCode == PlanningService.CREATE_PLANNING_FAILED) {

                    // System.out.println("dans le transfert : FAILED");
                    Log.i(alarmServiceTag, "Echec lors du transfert du planning");
                    message = "Echec lors du transfert du planning";
                    planningSuccess = false;

                    execute = false;
                    notifyService();

                } else if (resultCode == PlanningService.CREATE_PLANNING_SUCCESS) {

                    Log.i(alarmServiceTag, "Succes du transfert du planning");
                    Log.i(alarmServiceTag,
                            "Lancement du transfert des fiches de taches");
                    planningSuccess = true;

                    // appel du service
                    Intent intent = new Intent(context,
                            FicheTacheService.class);
                    intent.putExtra("receiver", new FicheTacheReceiver(
                            handlerFicheTache));
                    context.startService(intent);
                }
            }
        }

        /**
         * Classe qui permet de gérer un ResultReceiver personnalisé pour déclencher
         * les actions personnalisées lors du transfert des Fiches de tache
         * 
         * @author Anthony Fischer
         * 
         */
        private class FicheTacheReceiver extends ResultReceiver {
            public FicheTacheReceiver(Handler handler) {
                super(handler);
            }

            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                super.onReceiveResult(resultCode, resultData);

                if (resultCode == FicheTacheService.CREATE_FICHE_TACHE_FAILED) {

                    Log.i(alarmServiceTag,
                            "Echec lors du transfert des fiches de taches");
                    message = "Echec lors du transfert des fiches de taches";
                    ficheTacheSuccess = false;

                    execute = false;
                    notifyService();

                } else if (resultCode == FicheTacheService.CREATE_FICHE_TACHE_SUCCESS) {

                    Log.i(alarmServiceTag,
                            "Succès lors du transfert des fiches de taches");
                    ficheTacheSuccess = true;
                    execute = false;
                    message = "Succès lors du transfert automatique";
                    notifyService();
                }
            }
        }

        public class NetworkReceiver extends BroadcastReceiver {

            @Override
            public void onReceive(Context context, Intent intent) {

                Log.i(alarmServiceTag, "DANS le network receiver");
                boolean isWifiConnected = false, isWifiAvailable=false, is3GConnected = false, is3GAvailable = false;
                ConnectivityManager conn = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                isWifiConnected = networkInfo.isConnected();
                isWifiAvailable = networkInfo.isAvailable();

                NetworkInfo networkInfo3G = conn
                        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                is3GConnected = networkInfo3G.isConnected();
                is3GAvailable = networkInfo3G.isAvailable();

                try{
                    timer.schedule(tTask, INTERVAL);
                }catch(Exception e){
                    Log.e(alarmServiceTag, e.toString());
                }

                //System.out.println("etat du réseau : "+ networkInfo.getDetailedState());
                if (isWifiConnected && isWifiAvailable || is3GConnected && is3GAvailable) {

                    new TransfertAutoAsync().execute((Void[])null);

                }
            }
        }

        /**
         * Classe qui permet de lancer la vérification de la MAJ auto en mode Asynchrone afin de soulager le UIThread
         * @author Anthony Fischer
         *
         */
        private class TransfertAutoAsync extends AsyncTask<Void, Void, Void>{


            @Override
            protected Void doInBackground(Void... params) {
                if(!execute)
                    callTransfert();

                return null;
            }
            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);

            }

        }

}
1

There are 1 answers

0
Minifish On BEST ANSWER

I managed to reactivate the lock screen with a change in flags of newWakeLock i have before : PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP

and now : PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE | PowerManager.SCREEN_BRIGHT_WAKE_LOCK

i think it was the FULL_WAKE_LOCK was the problem