MipcaActivityCaptureコードを読む-surfaceviewのライフサイクル

13371 ワード

前言
ソースコードからhttp://blog.csdn.net/xiaanming/article/details/10163203surfaceDestroyedの呼び出しタイミングが分からないので、ネット上ではsurfaceviewが隠れている時に呼び出すと言われていますが、backキーを押して、homeキーを押して、スクリーンをロックして、もう一つの完全ではないactivityにジャンプしてsurfaceDestroyedを呼び出すかどうかはよく分かりません。だから、以下の関数の実行呼び出しプロセスを印刷ログで分析しました。多くのsurfaceviewのdemoの中でhasSurfaceを使っていますが、hasSurfaceは何の効果がありますか?以前は深い疑問を持っていました。コードをよく分析してやっと4つの方式のログが分かりました。
back
MipcaActivityCaptureに入るこのactivity 01-09 16:59:51.118:E/MipcaActivityCapture(2552):onCreate 01-09 16:59:51.141:E/MipcaActivityCapture(2552):onResume 01010-09 16:59:51.11:51.11:51.19:51.18:Acturure(2516:51.19:51.18:Acture 0101010101010101010101010101010101010101019:Acture(2516:51.19:51.19:51.19:Acture(259:59)):51.19:51.18:51.1(2552):surfaceCreated 01-09 16:59:51.259:E/MipcaActivityCapture(2552):initCamera
backを打つ
01-09 16:59:54.102:E/MipcaActivityCapture(2552):onPause 01-09 16:59:54.227:E/MipcaActivityCapture(2552):surfaceDestroyed 01-09:59:54.91:E/MipcactivityCapture Capture
ホーム
MipcaActivityCaptureに入るこのactivity 01-09 16:59:51.118:E/MipcaActivityCapture(2552):onCreate 01-09 16:59:51.141:E/MipcaActivityCapture(2552):onResume 01010-09 16:59:51.11:51.11:51.19:51.18:Acturure(2516:51.19:51.18:Acture 0101010101010101010101010101010101010101019:Acture(2516:51.19:51.19:51.19:Acture(259:59)):51.19:51.18:51.1(2552):surfaceCreated 01-09 16:59:51.259:E/MipcaActivityCapture(2552):initCamera
homeを注文する
01-09 17:28:47.196:E/Mipca ActivityCapture(26547):onPause 01-09 17:28:47.321:E/MipcaActivityCapture(26547):surfaceDestroyed 01-09 17:28:47.704:E/MipcantivityCapture Capture(26)
スクリーンをロックする
MipcaActivityCaptureこのactivity 01-09 17:31:03.259:E/MipcaActivityCapture(26655):onCreate 01-09 17:31:03.282:E/MipcaActivityCapture(26655):hasSurface=False 0101010101010101010101010101010 0 0-09 ppse 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010 2010-09:05:05:05:03.0.0.0.0.09:05:05:05:05:05:05:05:05:03.0.0.0.0.09:03.29:0101010101010101010101010101010101017:Capture(26655):surfaceCreated 01-09 17:31:03.38:E/MipcaActivityCapture(26655):initCamera
ロック画面(シャットダウンボタンを押す)
01-09 17:31:09.07:E/MipcaActivityCapture(26655):onPause 01-09 17:31:09.98:E/MipcaActivityCapture(26655):onStop
ロック解除(シャットダウンボタンを押す)
01-09 17:35:51.59:E/MipcaActivityCapture(27383):onResume 01-09:35:51.59:E/MipcaActivityCapture(27383):hasSurface=true 01-09 17:35:51.59:E/MipcactivityCapture 3
完全にカバーされていないactivityにジャンプします。
MipcaActivityCaptureに入るこのactivity 01-09 17:43:38.126:E/MipcaActivityCapture(27383):onCreate 01-09 17:43:38.165:E/MipcaActivityCapture(273838383):onResume 010101010-019:43:43:3838.09:38.09:38.09:38.09:38.05:38.05:38.05:38.07:38.05:38.05(Acture(Acture:38.05:38.05:38.05:38.05:38.07:38.05:38.05:38.05:38.05:38.05:38.05 Capture(27383):surfaceCreated 01-09 17:43:38.251:E/MipcaActivityCapture(27383):initCamera
完全にカバーされていないactivityに移動する場合
01-09 17:43:44.46:E/MipcaActivityCapture(27383):onPause 01-09 17:43:44.47:E/OtherActivity(27383):-----------------------------------onCreate-------------------------------01-09:44.47:E/OtherActivity(27.23)-------------------------------------------------------------------------------------------------------------------------------------------------------Star
backを押す
010-09 17:43:50.9554:E/OtherActivity(27383):--------------------------------------------------------01-09 17:43:509.77:E/MipcaActivityCapture(2738383):onReume 01-09:43:50.97:50.97:E/MipcactivaaactivititititititititititityCapturure Capture Capture Capture Capture Capture Capture(27272727272727272727272727272727272727272727272727272727272727272727272727277:01-09 17:43:51.954:E/OtherActivity(27383):--------------onStop----------01-09 17:43:51.954:E/OtherActivity(27383):-----------onDestroy------------------------------------------------------------------------------------------------
締め括りをつける
onPauseはCameramanager.get().close Driver();surfaceCreatedは普通OnResumeの後に呼び出します。backまたはhomeを押すとsurfaceDestroyedを呼び出します。surfaceDestroyedはOnPauseとOnStopの間で呼び出します。しかし、スクリーンロックはsurfaceDestroyedを起動しません。ロック解除もsurfaceCreatedを完全にカバーされていないactivityにジャンプさせません。surfaceDestroyedを調整しません。戻りもsurfaceCreatedを調整しません。
back/homeを押しても、スクリーンをロックしてもOnPauseを呼び出します。Camera Manager.get().close Driver()を招きます。だから今度activityに入る時はinitCameraが必要ですが、これはどうやって実現しますか?下記のコードを見て、HasSurfaceの役割が現れます。
        if(hasSurface) {
            initCamera(surfaceHolder);
        }else {
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
ロックスクリーンがロック解除された後、has Surfaceはtrueとなり、initCameraを呼び出します。backまたはhomeを押して戻ってきたらhasSurfaceはfalseです。ここではオンリーの後にsurfaceCreatedを呼び出します。surfaceCreatedではinitCameraを呼び出します。コードを少し変えました。
view plin copy
コードシートをコードコードコードコードコードで確認します。
私に派生したコードシート
package com.example.qr_codescan;  
  
import java.io.IOException;  
import java.util.Vector;  
  
import android.app.Activity;  
import android.app.AlertDialog;  
import android.app.Dialog;  
import android.content.Intent;  
import android.content.res.AssetFileDescriptor;  
import android.graphics.Bitmap;  
import android.media.AudioManager;  
import android.media.MediaPlayer;  
import android.media.MediaPlayer.OnCompletionListener;  
import android.os.Bundle;  
import android.os.Handler;  
import android.os.Vibrator;  
import android.util.Log;  
import android.view.SurfaceHolder;  
import android.view.SurfaceHolder.Callback;  
import android.view.SurfaceView;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.Toast;  
  
import com.google.zxing.BarcodeFormat;  
import com.google.zxing.Result;  
import com.mining.app.zxing.camera.CameraManager;  
import com.mining.app.zxing.decoding.CaptureActivityHandler;  
import com.mining.app.zxing.decoding.InactivityTimer;  
import com.mining.app.zxing.view.ViewfinderView;  
/** 
 * Initial the camera 
 * @author Ryan.Tang 
 */  
public class MipcaActivityCapture extends Activity implements Callback {  
  
    private static final String TAG = MipcaActivityCapture.class.getSimpleName();  
    private CaptureActivityHandler handler;  
    private ViewfinderView viewfinderView;  
    private boolean hasSurface;  
    private Vector decodeFormats;  
    private String characterSet;  
    private InactivityTimer inactivityTimer;  
    private MediaPlayer mediaPlayer;  
    private boolean playBeep;  
    private static final float BEEP_VOLUME = 0.10f;  
    private boolean vibrate;  
    Button btn;  
  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        Log.e(TAG, "onCreate");  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_capture1);  
        //ViewUtil.addTopView(getApplicationContext(), this, R.string.scan_card);  
        CameraManager.init(getApplication());  
        viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);  
          
        Button mButtonBack = (Button) findViewById(R.id.button_back);  
        mButtonBack.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                MipcaActivityCapture.this.finish();  
                  
            }  
        });  
        hasSurface = false;  
        inactivityTimer = new InactivityTimer(this);  
          
        btn=(Button) findViewById(R.id.btn1);  
        btn.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
  
                Intent intent=new Intent(MipcaActivityCapture.this, OtherActivity.class);  
                startActivity(intent);  
                  
            }  
        });  
    }  
  
    @Override  
    protected void onResume() {  
        Log.e(TAG, "onResume");  
        super.onResume();  
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);  
        SurfaceHolder surfaceHolder = surfaceView.getHolder();  
        Log.e(TAG, "hasSurface="+hasSurface);  
        if (hasSurface) {  
            initCamera(surfaceHolder);  
        } else {  
            surfaceHolder.addCallback(this);  
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
        }  
        decodeFormats = null;  
        characterSet = null;  
  
        playBeep = true;  
        AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);  
        if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {  
            playBeep = false;  
        }  
        initBeepSound();  
        vibrate = true;  
   
          
    }  
  
    @Override  
    protected void onPause() {  
        super.onPause();  
        if (handler != null) {  
            handler.quitSynchronously();  
            handler = null;  
        }  
        CameraManager.get().closeDriver();  
  
        Log.e(TAG, "onPause");  
    }  
  
    @Override  
    protected void onStop() {  
        // TODO Auto-generated method stub  
        super.onStop();  
        Log.e(TAG, "onStop");  
    }  
    @Override  
    protected void onDestroy() {  
        inactivityTimer.shutdown();  
        super.onDestroy();  
        Log.e(TAG, "onDestroy");  
    }  
      
    /** 
     *        
     * @param result 
     * @param barcode 
     */  
    public void handleDecode(Result result, Bitmap barcode) {  
        inactivityTimer.onActivity();  
        playBeepSoundAndVibrate();  
        String resultString = result.getText();  
        if (resultString.equals("")) {  
            Toast.makeText(MipcaActivityCapture.this, "Scan failed!", Toast.LENGTH_SHORT).show();  
        }else {  
            Intent resultIntent = new Intent();  
            Bundle bundle = new Bundle();  
            bundle.putString("result", resultString);  
            bundle.putParcelable("bitmap", barcode);  
            resultIntent.putExtras(bundle);  
            this.setResult(RESULT_OK, resultIntent);  
        }  
        MipcaActivityCapture.this.finish();  
    }  
      
    private void initCamera(SurfaceHolder surfaceHolder) {  
  
        Log.e(TAG, "initCamera");  
        try {  
            CameraManager.get().openDriver(surfaceHolder);  
        } catch (IOException ioe) {  
            return;  
        } catch (RuntimeException e) {  
            return;  
        }  
        if (handler == null) {  
            handler = new CaptureActivityHandler(this, decodeFormats,  
                    characterSet);  
        }  
    }  
  
    @Override  
    public void surfaceChanged(SurfaceHolder holder, int format, int width,  
            int height) {  
  
    }  
  
    @Override  
    public void surfaceCreated(SurfaceHolder holder) {  
  
        Log.e(TAG, "surfaceCreated");  
        if (!hasSurface) {  
            hasSurface = true;  
            initCamera(holder);  
        }  
  
    }  
  
    @Override  
    public void surfaceDestroyed(SurfaceHolder holder) {  
        hasSurface = false;  
  
        Log.e(TAG, "surfaceDestroyed");  
  
    }  
  
    public ViewfinderView getViewfinderView() {  
        return viewfinderView;  
    }  
  
    public Handler getHandler() {  
        return handler;  
    }  
  
    public void drawViewfinder() {  
        viewfinderView.drawViewfinder();  
  
    }  
  
    private void initBeepSound() {  
        if (playBeep && mediaPlayer == null) {  
            // The volume on STREAM_SYSTEM is not adjustable, and users found it  
            // too loud,  
            // so we now play on the music stream.  
            setVolumeControlStream(AudioManager.STREAM_MUSIC);  
            mediaPlayer = new MediaPlayer();  
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
            mediaPlayer.setOnCompletionListener(beepListener);  
  
            AssetFileDescriptor file = getResources().openRawResourceFd(  
                    R.raw.beep);  
            try {  
                mediaPlayer.setDataSource(file.getFileDescriptor(),  
                        file.getStartOffset(), file.getLength());  
                file.close();  
                mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);  
                mediaPlayer.prepare();  
            } catch (IOException e) {  
                mediaPlayer = null;  
            }  
        }  
    }  
  
    private static final long VIBRATE_DURATION = 200L;  
  
    private void playBeepSoundAndVibrate() {  
        if (playBeep && mediaPlayer != null) {  
            mediaPlayer.start();  
        }  
        if (vibrate) {  
            Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);  
            vibrator.vibrate(VIBRATE_DURATION);  
        }  
    }  
  
    /** 
     * When the beep has finished playing, rewind to queue up another one. 
     */  
    private final OnCompletionListener beepListener = new OnCompletionListener() {  
        public void onCompletion(MediaPlayer mediaPlayer) {  
            mediaPlayer.seekTo(0);  
        }  
    };  
}