相机&键盘锁

时间:2013-11-20 16:11:04

标签: android camera surfaceview keyguard

从服务中,我正在启动一个活动,此活动拍摄照片然后完成,

我的问题:

屏幕关闭时&服务启动此活动,活动不会拍照,相机看起来像未初始化,这是我正在使用的代码:

  @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...

  }

1 个答案:

答案 0 :(得分:0)

找到解决方案,答案:

 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...