1

SMSをモバイルに送信すると、カメラが自動的にビデオモードでオンになり、ビデオ録画が開始されます。モバイルがスリープモードでない場合、アプリケーションは正常に動作しますが、問題は、モバイルがスリープモードの場合、ビデオ録画が機能せず、スローされて で実行を停止することmMediaRecorder.prepare();です。これは私がSMSを受信するために使用しているコードです:

public void onReceive(final Context context, Intent intent) {
        // TODO Auto-generated method stub
        final Bundle bundle = intent.getExtras();

         pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); 
         wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My tag"); 
         wl.acquire();



         Thread timer = new Thread()
            { 
                public void run()
                { 
                    try { 


                        if(bundle != null)
                        {
                            Object[] pdus = (Object[]) bundle.get("pdus");

                            SmsMessage[] msg = new SmsMessage[pdus.length];

                            for(int i = 0; i <msg.length; i++)
                            {
                                msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
                                incomingMsg = msg[i].getMessageBody().toString(); 
                            }
                        }

                        System.out.println("incomingMsg :"+incomingMsg);

                        if(incomingMsg.length() > 0 && incomingMsg.trim().contains("start") )
                        {
                            System.out.println("starting activity");
                            Intent in =new Intent(context, CameraRecorderActivity.class);
                            in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(in);
                        }
                        else if(incomingMsg.length() > 0 && incomingMsg.trim().contains("stop"))
                        {
                            System.out.println("sending broadcast");
                            Intent in =new Intent(SMS_RECEIVE);
//                          in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            in.putExtra("key", "stop");
                            context.sendBroadcast(in);
                        }

                        sleep(100); 

                        } catch (InterruptedException e) { 
                            // TODO: handle exception 
                            }finally{ 
//                              wl.release(); 
                                } 
                            } 
            }; timer.start(); 


    }  

このコードは、ビデオを記録するために使用されます。 // ここで、問題が現実のものになります。

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create an instance of Camera.

    KeyguardManager manager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
    KeyguardLock lock = manager.newKeyguardLock(KEYGUARD_SERVICE);
    lock.disableKeyguard();

    mCamera = getCameraInstance();
    // Create preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    int i = R.id.videoview;
    Object o = this.findViewById(i);
    FrameLayout preview = (FrameLayout) o;
    preview.addView(mPreview);



    System.out.println("before run method.."+mCamera);




    new Handler().postDelayed(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub

            System.out.println("in run method.."+mCamera);
            if (isRecording) {
                // stop recording and release camera
                mMediaRecorder.stop();  // stop the recording
                releaseMediaRecorder(); // release the MediaRecorder object
                mCamera.lock();         // take camera access back from MediaRecorder

                // inform the user that recording has stopped
                setCaptureButtonText("Capture");
                isRecording = false;
            } else {
                // initialize video camera
                if (prepareVideoRecorder()) {
                    // Camera is available and unlocked, MediaRecorder is prepared,
                    // now you can start recording
                     System.out.println("media recorder before start...");
                    mMediaRecorder.start();
                    System.out.println("media recorder start...");
                    // inform the user that recording has started
                    setCaptureButtonText("Stop");
                    isRecording = true;
                } else {
                    // prepare didn't work, release the camera
                    releaseMediaRecorder();
                    // inform user
                }
            }

        }

    }, 10000);


 mHandleMessageReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String task = intent.getExtras().getString("key");
            System.out.println("receiving broadcast :"+task);

            if(task.equals("stop"))
            {
                if (isRecording) {
//                  unregisterReceiver(mHandleMessageReceiver);
                    // stop recording and release camera
                    mMediaRecorder.stop();  // stop the recording
                    releaseMediaRecorder(); // release the MediaRecorder object
                    mCamera.lock();         // take camera access back from MediaRecorder

                    // inform the user that recording has stopped
                    setCaptureButtonText("Capture");
                    isRecording = false;


                    finish();
                } else {
                    // initialize video camera
                    if (prepareVideoRecorder()) {
                        // Camera is available and unlocked, MediaRecorder is prepared,
                        // now you can start recording
                        mMediaRecorder.start();

                        // inform the user that recording has started
                        setCaptureButtonText("Stop");
                        isRecording = true;
                    } else {
                        // prepare didn't work, release the camera
                        releaseMediaRecorder();
                        // inform user
                    }
                }
            }
        }
    };

    registerReceiver(mHandleMessageReceiver,new IntentFilter(SMS_RECEIVE));

 // Add a listener to the Capture button
    captureButton = (Button) findViewById(R.id.mybutton);
    captureButton.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isRecording) {
                    // stop recording and release camera
                    mMediaRecorder.stop();  // stop the recording
                    releaseMediaRecorder(); // release the MediaRecorder object
                    mCamera.lock();         // take camera access back from MediaRecorder

                    // inform the user that recording has stopped
                    setCaptureButtonText("Capture");
                    isRecording = false;
                } else {
                    // initialize video camera
                    if (prepareVideoRecorder()) {
                        // Camera is available and unlocked, MediaRecorder is prepared,
                        // now you can start recording
                        mMediaRecorder.start();

                        // inform the user that recording has started
                        setCaptureButtonText("Stop");
                        isRecording = true;
                    } else {
                        // prepare didn't work, release the camera
                        releaseMediaRecorder();
                        // inform user
                    }
                }
            }
        }
    );
}

public void setCaptureButtonText(String s) {
    captureButton.setText(s);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // The Surface has been created, now tell the camera where to draw the preview.
    try {
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    } catch (IOException e) {
        Log.d(TAG, "Error setting camera preview: " + e.getMessage());
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
//    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");


    File rootDir = Environment.getExternalStorageDirectory();
    File path = new File(rootDir.getAbsolutePath(),"/Srinivas/");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.
    // Create the storage directory if it does not exist
    if (! path.exists()){
        if (! path.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(path + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    } else if(type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(path.getPath() + File.separator +
        "VID_"+ timeStamp + ".3gp");
    } else {
        return null;
    }

    System.out.println("mediaFile :"+mediaFile);
    return mediaFile;
}

private boolean prepareVideoRecorder(){

    try {
    //mCamera = getCameraInstance();
    mMediaRecorder = new MediaRecorder();
    mCamera.stopPreview() ;
    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);
    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//    mMediaRecorder.setVideoSize(240, 240);
    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
    // Step 4: Set output file
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());

    System.out.println("1");
    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
    // Step 6: Prepare configured MediaRecorder
    System.out.println("2");
        mMediaRecorder.prepare();
        System.out.println("3");
    } catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        e.printStackTrace();
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
        e.printStackTrace();
        releaseMediaRecorder();
        return false;
    } catch(Exception e)
    {
        e.printStackTrace();
    }
    return true;
 }
4

1 に答える 1

0

これは仕様によるものです。カメラはスリープ状態のデバイスでは機能しません。SMS の着信でアプリが起動する場合、アクティビティを開始すると、画面がオンになります。

そのためにはActivity.onCreate()、次のフラグを設定する必要があります。

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
于 2013-10-09T09:02:00.467 に答える