五、项目结构

常用包名定义及含义

  • com.company.test.system(application service等)
  • com.company.test.thread(thread基类等)
  • com.company.test.tk(ip地址的获取类)
  • com.company.test.util(定义一些接口 结构等)
  • com.company.test.pack(socket通讯包类)
  • com.decoder.util(H264解码库 必须和jni中定义的包名一致)
image

MyActivity的父类封装

  • 所有的Activity都继承于它
  • 定义公共的成员变量和函数
  • 可做退出和打开的特殊定制效果
package com.company.test.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;

import com.company.test.R;

public class MyActivity extends AppCompatActivity {

    public static Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        mContext = this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            animFinish();
        }

        return super.onKeyDown(keyCode, event);
    }

    private void animFinish(){
        finish();
    }

    //activity进入  从右边进入
    public void overridePendingTansitionEnter(){
        this.overridePendingTransition(R.anim.in_form_right,R.anim.out_right_to_left);
    }

    //activity退出
    public void overridePendingTansitionExit(){
        this.overridePendingTransition(R.anim.in_form_right,R.anim.out_right_to_left);
    }
}

SafeThread父类封装

  • 所有thread类的父类
  • 定义开启和关闭机制 优化代码
package com.company.test.thread;

import android.util.Log;

/**
 * 所有线程内的基类
 */
public class SafeThread extends Thread {

    private static final String TAG = "SafeThread";

    //可以作为线程运行标示 避免重复启动
    protected boolean isRunFlg = false;

    public void setRunFlg(boolean flag) {
        isRunFlg = flag;
    }

    public void safeStart(){
        if (isRunFlg)
            return;
        setRunFlg(true);
        start();
    }

    public void safeStop(){
        if (!isRunFlg) return;
        setRunFlg(false);
        tryJoin();
    }

    public void tryJoin(){
        try {
            interrupt();
            join();
        }catch (InterruptedException e){
            Log.i(TAG, "tryJoin: ---" + e.getMessage());
        }
    }
}

通讯过程

  • 手机设备端登陆login
  • 手机设备端验证verify
  • 设备端视频请求(请求成功后返回videoId)
  • 设备端另开一个socket2
  • socket2发送视频请求(带上前面接收到的videoId)
  • 服务器接收到视频请求指令后开始发送视频

你可能感兴趣的:(五、项目结构)