Which one is better between using static var and the parameter of the transfer object?

I want to record APP for screen recording, there are two ways: RecordHelper_Method_A and RecordHelper_Method_B.

In RecordHelper_Method_A, I define mMediaRecorder, MediaProjection mMediaProjection and mVirtualDisplay as a static var, it's easy to call it like StartRecord( mContext, requestCode, resultCode,data) , StopRecord() .

and in RecordHelper_Method_B ​​I need to define mMediaRecorder, MediaProjection mMediaProjection in the main class Activity and pass parameters when calling StartRecord(mMediaRecorder, mMediaProjection,mVirtualDisplay) , 'StopRecord (mMediaRecorder, mMediaject` a bit difficult ...).

I don’t know which one is better, and I don’t know anymore whether this static var can be released correctly in RecordHelper_Method_A when I finish APP.

By the way, if you have a better way, could you tell me? Thanks!

RecordHelper_Method_A

 public class RecordHelper_Method_A { private static MediaRecorder mMediaRecorder; private static MediaProjection mMediaProjection; private static VirtualDisplay mVirtualDisplay; public static void StartRecord(Context mContext,int requestCode, int resultCode, Intent data){ mMediaRecorder = new MediaRecorder(); initRecorder(); MediaProjectionManager mProjectionManager = (MediaProjectionManager) mContext.getSystemService(Context.MEDIA_PROJECTION_SERVICE); mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data); mVirtualDisplay=mMediaProjection.createVirtualDisplay("MainActivity", 400,600, 300, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mMediaRecorder.getSurface(), null, null); MediaProjectionCallback mMediaProjectionCallback = new MediaProjectionCallback(); mMediaProjection.registerCallback(mMediaProjectionCallback, null); mMediaRecorder.start(); } public static void StopRecord(){ mMediaProjection=null; mMediaRecorder.stop(); mMediaRecorder.reset(); mMediaRecorder.release(); mVirtualDisplay.release(); } private static void initRecorder() { mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); //... } private static class MediaProjectionCallback extends MediaProjection.Callback { @Override public void onStop() { mMediaRecorder.stop(); } } } 

RecordHelper_Method_B

 public class RecordHelper_Method_B { public static void StartRecord(MediaRecorder mMediaRecorder,MediaProjection mMediaProjection,VirtualDisplay mVirtualDisplay){ initRecorder(mMediaRecorder); mVirtualDisplay=mMediaProjection.createVirtualDisplay("MainActivity", 400,600, 300, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mMediaRecorder.getSurface(), null, null); MediaProjectionCallback mMediaProjectionCallback = new MediaProjectionCallback(mMediaRecorder); mMediaProjection.registerCallback(mMediaProjectionCallback, null); mMediaRecorder.start(); } public static void StopRecord(MediaRecorder mMediaRecorder,MediaProjection mMediaProjection,VirtualDisplay mVirtualDisplay){ mMediaProjection=null; mMediaRecorder.stop(); mMediaRecorder.reset(); mMediaRecorder.release(); mVirtualDisplay.release(); } private static void initRecorder(MediaRecorder mMediaRecorder) { mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); //... } private static class MediaProjectionCallback extends MediaProjection.Callback { MediaRecorder mMediaRecorder; public MediaProjectionCallback(MediaRecorder mMediaRecorder){ this.mMediaRecorder=mMediaRecorder; } @Override public void onStop() { mMediaRecorder.stop(); } } } 
+8
java android
source share
5 answers

I would suggest Objects do not live statically . No matter what object or resource you create, it must die when the action dies. Therefore, creating these resources in an activity and then transferring it is good.

In addition, I see that in both methods that you proposed, the methods themselves are static. It is better to create an object for the class, and then pass resources to this object. This will prevent a memory leak. You can do something similar in your work.

 RecordHelper_Method_B helper = new RecordHelper_Method_B (); helper.StopRecord(mMediaRecorder,mMediaProjection,mVirtualDisplay); 

It makes life easier. As soon as your Activity dies, there will also be a helper object. Thus, all resources will be released for sure.

+9
source share

In general, if you want to use static functions, you should not define static variables from the outside. This will result in poor performance. In addition, the static method should be sufficient "by itself" for storage and maintenance.

As in the Android context, you can read this article for more information / tips: http://developer.android.com/training/articles/perf-tips.html

+3
source share

Dependency injection is better all the time (you see the classic name). so release static variables and find a way to feed your methods, etc. etc.

using this means more reuse of your code, more readability, shorter lifespan of objects - when they die, when the method is executed, etc. etc.

+1
source share

If static variables are not used, then why were they introduced. Hi guys, I think all this is going to be learned from experience how to use static variables and process them efficiently. I just want to say that just do not accept this, since using statics can be harmful, and using them without knowledge is harmful. :)

+1
source share

Static variables are good if they do not contain a mutable state (i.e. constants or read-only values). Otherwise, these are global variables, and therefore their use is considered bad practice. Why?
From Wikipedia or Evil Static Variables :

  • Reduce testability.
  • Increases complexity (depends on encapsulation)
  • Name exchange (more or less true, language dependent)

Even static behavior can be problematic if it refers to unique resources and then is not available for sharing and verification (database, file system, sockets, etc.)

+1
source share

All Articles