-1

是否有可能使用HandlerThread其內部主UI線程上進一步啓動的服務的BroadcastReceiver 的onReceive方法??HandlerThread內部的BroadcastReceiver

我的目標是使用HandlerThread的onReceive方法,使服務得到在單獨的線程啓動。

但不知道如何實現它。

任何提示?

感謝

編輯:服務類

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:logcat的

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. 
+0

爲什麼你想在一個單獨的線程中調用'startService()'?請解釋。 –

+0

因爲每次運行應用程序時,我的日誌中都會有一條消息。「主線程上的太多東西」以及我的設備速度變慢。 –

+0

你不會通過在另一個線程中調用'startService()'來解決這個問題。你的問題在別的地方。發佈你的'onStart()'或'onStartCommand()'方法的代碼。 –

回答

0

參考BroadcastReceiver,您可以onReceive方法使用HandlerThread

接收器生命週期

任何需要異步操作不可用,因爲你需要從恢復函數來處理異步操作,但此時BroadcastReceiver不再處於活動狀態,因此係統可以在異步操作完成之前自由地終止其進程。

流程生命週期

......這意味着更長運行的操作你會經常結合使用服務與一個BroadcastReceiver以保持含活性處理爲的整個時間你的操作。

因此請遵守ANDROID API GUIDES:要運行更長的運行操作時調用onReceive,你應該啓動一個服務,然後創建在服務另一個線程。

請參閱Extending the Service class的示例代碼,它會創建HandlerThread,並在接收到消息時僅休眠5秒鐘。

+0

那麼任何建議,如何在內部進行長時間運行的操作,除了服務接收爲我使用的服務仍然沒有得到想要的結果 –

+0

如果執行長時間運行是您的目的,爲什麼服務不能滿足您的期望?你如何實現它? – li2

+0

Exatly.There你。我在尋找解決這個問題,爲什麼會出現'的應用可以執行其主要thread'太多的工作,雖然我的應用程序工作正常,但我的設備減慢 –