내부는 상기 메인 UI 스레드에서 서비스 시작 브로드 캐스트 리시버의 onReceive있어서 내부 HandlerThread 사용할 수있다 ??HandlerThread 브로드 캐스트 리시버
내 목표는 서비스가 별도의 스레드에서 시작됩니다 있도록 onReceive 방법 내부 HandlerThread을 사용하는 것입니다.
하지만 어떻게 구현해야할지 모르겠다.
힌트가 있습니까?
감사
편집 : 서비스 클래스
public class BackgroundVideoRecorder extends Service implements SurfaceHolder.Callback {
public WindowManager windowManager;
public SurfaceView surfaceView;
public Camera camera = null;
public MediaRecorder mediaRecorder = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
// Create new SurfaceView, set its size to 1x1, move it to the top left corner and set this service as a callback
windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
surfaceView = new SurfaceView(getApplicationContext());
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
1, 1,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT
);
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
windowManager.addView(surfaceView, layoutParams);
surfaceView.getHolder().addCallback(this);
}
// Method called right after Surface created (initializing and starting MediaRecorder)
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA) == PackageManager.PERMISSION_GRANTED) {
camera = Camera.open();
}else{
// should show permission
}
} else {
camera = Camera.open();
}
mediaRecorder = new MediaRecorder();
camera.unlock();
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setCamera(camera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P));
mediaRecorder.setOutputFile(
Environment.getExternalStorageDirectory() + "/" +
DateFormat.format("yyyy-MM-dd_kk-mm-ss", new Date().getTime()) +
".mp4"
);
try {
mediaRecorder.prepare();
} catch (Exception e) {
}
mediaRecorder.start();
} catch (Exception e) {
}
}
// Stop recording and remove SurfaceView
@Override
public void onDestroy() {
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
camera.lock();
camera.release();
windowManager.removeView(surfaceView);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
}
}
내가 제대로 서비스를 사용하고 있지 않다 생각합니다. 도와주세요.
EDIT2 : 로그 캣
08/11 18:00:20: Launching app
$ adb push F:\AndroidStudioWorkspace\MyApplication2\app\build\outputs\apk\app-debug.apk /data/local/tmp/com.example.jatin.myapplication2
$ adb shell pm install -r "/data/local/tmp/com.example.jatin.myapplication2"
pkg: /data/local/tmp/com.example.jatin.myapplication2
Success
$ adb shell am start -n "com.example.jatin.myapplication2/com.example.jatin.AgentSpy.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 7445 on device xiaomi-mi_4i-a67ae459
D/###BVRREC###: true
I/MediaRecorderJNI: prepare: surface=0x5583ad0d30
I/Choreographer: Skipped 64 frames! The application may be doing too much work on its main thread.
D/###BVRREC###: false
V/RenderScript: Application requested CPU execution
V/RenderScript: 0x558342fa80 Launching thread(s), CPUs 8
D/###BVRREC###: true
I/MediaRecorderJNI: prepare: surface=0x5583b23330
I/Choreographer: Skipped 42 frames! The application may be doing too much work on its main thread.
D/###BVRREC###: false
Application terminated.
왜 'startService()'를 별도의 스레드에서 호출 하시겠습니까? 설명을 부탁드립니다. –
응용 프로그램을 실행할 때마다 내 로그 고양이에 메시지가 있습니다. "주 스레드에서 너무 많이 wokring"하고 장치가 느려집니다. –
다른 스레드에서'startService()'를 호출하여 문제를 해결할 수 없습니다. 문제는 다른 곳에서 발생합니다. 'onStart()'또는'onStartCommand()'메소드의 코드를 게시하십시오. –