关于Android Activity

时间:2012-12-24 08:03:35

标签: android android-activity

我正面临与Activity相关的大问题。我的方案如下:

我启动活动A,点击按钮然后从图库中选择了一张图片,然后在onActivityResult中调用startActivity(B)

现在,在我的活动B中,在A中选择的图像已显示在显示的ListView上。

现在点击一个名为startActivityforResult(Recorder class)的按钮,打开了一个新的活动,这是一个自定义录像机活动。

现在进入活动记录器点击停止按钮mediaRecorder停止,释放并保存视频,然后在setResult后再次将活动B转到onActivityResult()。 但问题出在这里:

我在这里找到了两个不同的案例:

  1. 第一个是我将Recorder类声明为Landscape:

    此处点击Recorder类中的停止按钮,我的屏幕变黑,几秒后活动A开始。

  2. 第二个是当我将Recorder类的方向更改为Portrait模式时:

    此处点击记录器类活动B中的停止按钮启动(无黑屏),但几秒后活动A再次启动。

  3. 我在这里观察到的另一件事是:

    • 我在活动B中将startActivityForResult()替换为startActivity(),从记录活动i替换为startActivity(B),其中显示了活动B, 但是这里有一些新的事情发生在几秒钟之后,同样的活动B再次开始。
    • 我确信这只是因为MediaRecorder,因为当我评论来自Recorder课程的所有媒体录制器代码时,它很好。

    我的录音机类课程在这里:

    public class Recorder extends Activity implements SurfaceHolder.Callback{
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            mediaRecorder = new MediaRecorder();
            setContentView(R.layout.recorde);
            initMediaRecorder();
    
            timer = new Timer();
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    if(counter<=10)
                        counter++;
                    //Android UI get Updated continouly
                    else {
                        // If condition full filled the timer will stop here
                        mediaRecorder.stop();
                        mediaRecorder.reset();
                        mediaRecorder.release();
                        timer.cancel();
                        Recorder.this.finish();
                        Intent i = new Intent(Recorder.this,B.class);
                        setResult(Activity.RESULT_OK, i.putExtra("returnedVideo", strPath));
                    }
                } 
            };
            timer.schedule(timerTask, 1000, 1000);
    
            SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview);
            surfaceHolder = myVideoView.getHolder();
            surfaceHolder.addCallback(this);
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
            myButton = (Button)findViewById(R.id.mybutton);
            myButton.setOnClickListener(myButtonOnClickListener);
        }
    
        private Button.OnClickListener myButtonOnClickListener
        = new Button.OnClickListener(){
    
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mediaRecorder.stop();
                mediaRecorder.reset();
                mediaRecorder.release();
                timer.cancel();
                Recorder.this.finish();
    
                Intent i = new Intent(Recorder.this,B.class);
                setResult(Activity.RESULT_OK, i.putExtra("returnedVideo", strPath));
            }};
    
            public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
    
            }
            public void surfaceCreated(SurfaceHolder arg0) {
                // TODO Auto-generated method stub
                prepareMediaRecorder();
            }
            public void surfaceDestroyed(SurfaceHolder arg0) {
                // TODO Auto-generated method stub
    
                mediaRecorder.stop();
                mediaRecorder.release();
                //Recorder.this.finish();
            }
    
            @Override
            public void onBackPressed() {
                // TODO Auto-generated method stub
                super.onBackPressed();
    
                mediaRecorder.stop();
                mediaRecorder.release();
            }
    
            private void initMediaRecorder(){
                Random genraotr = new Random();
                int n = 10000;
                n = genraotr.nextInt(n);            
    
                mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
                mediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
                CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
                mediaRecorder.setProfile(camcorderProfile_HQ);
                mediaRecorder.setOutputFile("/sdcard/my_video"+n+".mp4");
                strPath = "/sdcard/my_video"+n+".mp4";
                mediaRecorder.setMaxDuration(10000); // Set max duration 10 sec.
                mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M
            }
    
            private void prepareMediaRecorder(){
                mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
                try {
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    }
    

2 个答案:

答案 0 :(得分:1)

我没有看到任何可以保存和恢复Activity实例状态的代码。 Android可能会在将另一个活动置于顶部时销毁并重新创建活动,并且当您重新定向设备时它也会重新启动活动。在这两种情况下,你似乎确实遇到了问题。

Android然后尝试恢复活动,但您需要通过存储和恢复您的状态来提供帮助。默认情况下不会恢复太多,尤其是您自己的字段只能获取空值。

实施onSavedInstanceState,添加可能对您的计划继续工作很重要的提供的捆绑(地图)。然后检查onCreate参数savedInstanceState是否为null,如果是,则从该捆绑包中恢复您认为重要的内容。另外,实施onRestoreInstanceState

以下是演示此问题的代码。它是一个简单的应用程序,它有一个String类型的状态变量。启动程序并尝试将设备从纵向重定向到横向。您的消息文本将更改为“我的状态为null但现在已恢复为13c140ff7e6”。因此,第二次调用create(),状态字段为null,现在必须从传递的bundle中恢复。同时,静态字段中的值很可能将被保留(在某些情况下也可能会消失),表明这不是应用程序的完全冷启动。这可以解释为什么简单的重新定向可能会使不知道这些事情的应用程序崩溃。为结果启动另一个活动可能会产生类似的效果。该代码还演示了如何解决问题。

package com.example.state;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

String myState;

static String staticString;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView sv = (TextView) findViewById(R.id.main);

    if (savedInstanceState != null) {
        String state = savedInstanceState.getString("myState");
        sv.setText("My state was " + myState + " but now restored to "
                + state+", static "+staticString);
        myState = state;
    } else {
        String state = Long.toHexString(System.currentTimeMillis());
        sv.setText("My state: " + myState + ", cold start, now set to "+state);
        myState = state;
        staticString = state;
    }
}

protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    myState = savedInstanceState.getString("myState");
}

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("myState", myState);
}
}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
     />

</RelativeLayout>

答案 1 :(得分:1)

您的刻录机课程似乎存在问题。请尝试使用此代码:

public class AndroidVideoCapture extends Activity{

    private Camera myCamera;
    private MyCameraSurfaceView myCameraSurfaceView;
    private MediaRecorder mediaRecorder;

    Button myButton;
    SurfaceHolder surfaceHolder;
    boolean recording;  
    public static String strPath;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        recording = false;

        setContentView(R.layout.main);

        //Get Camera for preview
        myCamera = getCameraInstance();
        if(myCamera == null){
            Toast.makeText(AndroidVideoCapture.this, 
                    "Fail to get Camera", 
                    Toast.LENGTH_LONG).show();
        }

        myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
        FrameLayout myCameraPreview = (FrameLayout)findViewById(R.id.videoview);
        myCameraPreview.addView(myCameraSurfaceView);

        myButton = (Button)findViewById(R.id.mybutton);
        myButton.setOnClickListener(myButtonOnClickListener);
    }

    Button.OnClickListener myButtonOnClickListener
    = new Button.OnClickListener(){

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(recording){
                // stop recording and release camera
                mediaRecorder.stop();  // stop the recording
                releaseMediaRecorder(); // release the MediaRecorder object
                Intent i = new Intent(AndroidVideoCapture.this,TakeAnotherPhoto.class);
                i.putExtra("returnedVideo", strPath);
                setResult(Activity.RESULT_OK, i);

                //Exit after saved
                finish();
            }else{

                //Release Camera before MediaRecorder start
                releaseCamera();

                if(!prepareMediaRecorder()){
                    Toast.makeText(AndroidVideoCapture.this, 
                            "Fail in prepareMediaRecorder()!\n - Ended -", 
                            Toast.LENGTH_LONG).show();
                    finish();
                }

                mediaRecorder.start();
                recording = true;
                myButton.setText("STOP");
            }
        }};

    private Camera getCameraInstance(){
        // TODO Auto-generated method stub
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }

    private boolean prepareMediaRecorder(){

        Random genraotr = new Random();
        int n = 10000;
        n = genraotr.nextInt(n);    

        myCamera = getCameraInstance();
        mediaRecorder = new MediaRecorder();

        myCamera.unlock();
        mediaRecorder.setCamera(myCamera);

        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

        mediaRecorder.setOutputFile("/sdcard/auction_video"+n+".mp4");
        strPath = "/sdcard/auction_video"+n+".mp4";
        mediaRecorder.setMaxDuration(10000); // Set max duration 10 sec.
        mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M

        mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());

        try {
            mediaRecorder.prepare();
        } catch (IllegalStateException e) {
            releaseMediaRecorder();
            return false;
        } catch (IOException e) {
            releaseMediaRecorder();
            return false;
        }
        return true;

    }

    protected void onPause() {
        super.onPause();
        releaseMediaRecorder();       // if you are using MediaRecorder, release it first
        releaseCamera();              // release the camera immediately on pause event
    }

    private void releaseMediaRecorder(){
        if (mediaRecorder != null) {
            mediaRecorder.reset();   // clear recorder configuration
            mediaRecorder.release(); // release the recorder object
            mediaRecorder = null;
            myCamera.lock();           // lock camera for later use
        }
    }

    private void releaseCamera(){
        if (myCamera != null){
            myCamera.release();        // release the camera for other applications
            myCamera = null;
        }
    }

    public class MyCameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback{

        private SurfaceHolder mHolder;
        private Camera mCamera;

        public MyCameraSurfaceView(Context context, Camera camera) {
            super(context);
            mCamera = camera;

            // Install a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed.
            mHolder = getHolder();
            mHolder.addCallback(this);
            // deprecated setting, but required on Android versions prior to 3.0
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int weight,
                int height) {
            // If your preview can change or rotate, take care of those events here.
            // Make sure to stop the preview before resizing or reformatting it.

            if (mHolder.getSurface() == null){
              // preview surface does not exist
              return;
            }

            // stop preview before making changes
            try {
                mCamera.stopPreview();
            } catch (Exception e){
              // ignore: tried to stop a non-existent preview
            }

            // make any resize, rotate or reformatting changes here

            // start preview with new settings
            try {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();

            } catch (Exception e){
            }
        }

        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            // The Surface has been created, now tell the camera where to draw the preview.
            try {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            } catch (IOException e) {
            }
        }

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

        }
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();

        mediaRecorder.stop();  // stop the recording
        releaseMediaRecorder(); // release the MediaRecorder object               
        //Exit after saved
        finish();
    }

//  public void showMessage(String title, String message, final Context context){
//      try
//      {
//          AlertDialog.Builder alt_bld = new AlertDialog.Builder(context);
//          alt_bld.setTitle(title);
//          alt_bld.setMessage(message);
//          alt_bld.setCancelable(false);
//          alt_bld.setNeutralButton("OK", new DialogInterface.OnClickListener() {
//
//              public void onClick(DialogInterface dialog, int which) {
//                  // TODO Auto-generated method stub
//                  dialog.dismiss();
//                  
//                  Intent i = new Intent(AndroidVideoCapture.this,TakeAnotherPhoto.class);
//                  i.putExtra("returnedVideo", strPath);
//                  setResult(Activity.RESULT_OK, i);
//              }
//          });
//          alt_bld.show();
//      }
//      catch (Exception e) {
//          // TODO: handle exception
//      }
//  }
}

让我知道它是否有效。

由于