2014-11-17 5 views
0

AlarmManager에서 호출 한 코드에 문제가 있습니다. 데이터를 웹 서비스로 전송하기 위해 wakeLock을 호출하여 데이터를 전송합니다. 내 wakelock이 잘 실행되었으며 업로드 후 메서드 다운로드를 호출합니다. 하지만 그 후에는 장치가 더 이상 전원을 차단하지 않습니다. 버튼을 눌러 설명을 이해할 수 없습니다. (설명하기 위해 장치 화면이 꺼져 있지만 전원 버튼을 다시 누르면 잠금 화면이 더 이상 나타나지 않습니다. 잠금 해제 됨) 누군가 나를 도울 수 있다면 WakeLock 이후 장치가 잠기지 않습니다.

내 코드 :

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); 

      } 

     } 

} 

답변

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 문제라고 생각합니다.

관련 문제