主进程与子进程的通信(handler)

  1. 首先是主进程与子进程的通信实现:

    1. 首先在oncreat中:
      new CustomThread().start();

    2. 需要使用的时候:

      mHandler.obtainMessage(PLAY_VIDEO).sendToTarget();
    3. 主进程处理:
      class CustomThread extends Thread {
              @Override
              public void run() {
                  Log.d(TAG, "darren ---> CustomThread...");
                  Looper.prepare();
                  mHandler = new Handler(){
                      public void handleMessage (Message msg) {
                          switch(msg.what) {
                          case PLAY_VIDEO :
                              novideo.setVisibility(View.INVISIBLE);              
                              Log.d(TAG, "darren ---> createResumeVideoThread restart to resume play...");
                              if (video_path != null) {
                                  video_view.setVideoPath(video_path);
                                  video_view.seekTo(MainActivity.video_pos);
                                  video_view.start(); 
                                  Log.d(TAG, "darren ---> start end...");
                              }
                          break;
      
                          case STOPED_VIDEO:
                              Log.d(TAG, "darren --->  case STOPED_VIDEO...");
                              video_view.stopPlayback();
                              Log.d(TAG, "darren --->  stop video...");
      //                          video_view.setVisibility(View.INVISIBLE);
      //                          Log.d(TAG, "darren --->  invisible view...");
                              novideo.setVisibility(View.VISIBLE);
                              Log.d(TAG, "darren --->  set picture...");
                          break;
      
                          case PAUSED_VIDEO:
                              video_view.pause();
      
                          break;
      
                          }
                      }
                  };
                  Looper.loop();
              }
          }

你可能感兴趣的:(主进程与子进程的通信(handler))