androidcamerasurfaceviewkeyguard

Camera & KeyGuard


From a Service, i am launching an Activity, this Activity Take a Picture then Finish,

My Problem :

When Screen is Off & the Service Start this Activity, the Activity does not Take Picture at All, Camera look like Not initialized, This is the Code i am using :

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);      
        setContentView(R.layout.main);


    ////Using those Flags to turn the Screen On & Dismiss the Keyguard & it Turn the Screen On with Success
         this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
         this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
         this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);


      ////Tried Sleep Method after Turning the Screen On with no Luck
           try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();

        if(camera != null){
               camera.release();
               camera = null;
               }

 ///Camera Initialization & Take Picture but Nothing Happen if this Activity was Started when Screen was Off...

 }


        public void onPause(){
        super.onPause();
        Log.e("TAG", "onPause");
        if(camera != null){
           camera.release();
           camera = null;
           }

       }


         @Override
    public void onResume(){
        Toast.makeText(this, "On Resume", Toast.LENGTH_SHORT).show();
    }


        public void onDestroy(){

           super.onDestroy();
           if(camera != null){ surfaceHolder.removeCallback(this);
           camera = null;}

       }


    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "surfaceCreated", Toast.LENGTH_SHORT).show();
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

   ///Camera Initialization...

  }

Solution

  • Found the Solution, Answer :

     PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
    
            boolean isScreenOn = pm.isScreenOn();
    
    
            try
            {
                // give 500ms for device to wake up
                Thread.sleep(500);
            }
            catch (InterruptedException e)
            {
            }
    
           /////Build.VERSION.SDK_INT >= 11 because this.recreate() Method is only Avaible since Android 3.0
            if(!isScreenOn && Build.VERSION.SDK_INT >= 11){
    
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE | PowerManager.ACQUIRE_CAUSES_WAKEUP, "AQUIRED");
            wl.acquire();
    
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    
            try
            {
                // give 500ms for device to wake up
                Thread.sleep(600);
            }
            catch (InterruptedException e)
            {
            }
    
    
    
           ////Important, if Screen was Off/KeyGuard Active, we ReCreate the Activity
            this.recreate();
    
    
    
            }
    
    
            if(camera != null){
                   camera.release();
                   camera = null;
                   }
    
      ////Camera Initialization...