WakeLock后设备未锁定

时间:2014-11-17 15:06:51

标签: android alarmmanager wakelock

我遇到一个由AlarmManager调用的代码有问题。 我调用wakeLock来执行任务,以便将数据传输到Web服务 我的唤醒锁执行得很好,在我上传后,我调用方法发布 但在那之后,我的设备不再用电源按钮锁定,我不明白为什么......(为了解释,设备屏幕关闭但是当我再次按下时锁定屏幕不再出现电源按钮,它是解锁的) 如果有人可以帮助我

我的代码:

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 个答案:

答案 0 :(得分:0)

我设法通过更改newWakeLock的标志来重新激活锁定屏幕 我之前有:PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP

现在:PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE | PowerManager.SCREEN_BRIGHT_WAKE_LOCK

我认为是FULL_WAKE_LOCK是问题

相关问题