美文网首页
面向android设备之电视盒子

面向android设备之电视盒子

作者: 茴香豆的第五种写法 | 来源:发表于2017-03-28 16:56 被阅读0次

    电视盒子中比较常见的一个难点总结如下:

    1:判断是自启动还是与服务端测试

    2:没有网络情况下,怎么在启动后通过adb shell设置静态IP

    3:Net有线网络没有插入,怎么连接指定wifi后进行与服务端长连接

    4:判断设备优盘和外置SDCard挂载情况

    5:通过HDMI播放外置T卡的视频

    1:判断是自启动还是与服务端测试

    这里可以通过adb shell命令操作,在启动的Activity中判断是否有该值存在,有则为自启动检测

    如下:adb shell am start -n com.dongzhou.stb/com.dongzhou.stb.ui.MainAcivity -e startup "labView"

    然后在onCreate中判断代码:

    Intent intent = getIntent();

    if (intent != null) {

    if (intent.hasExtra("startup")) {

    if (intent.getStringExtra("startup").equals("labview")) {

    // 不通信,自启动

    return;

    }

    } else {

    // 判断,如果有线网络为空,则链接wifi

    }

    3:Net有线网络没有插入,怎么连接指定wifi后进行与服务端长连接

    首先判断设备中的有线网,3G网,和wifi的状态;

    ConnectivityManager cManager = (ConnectivityManager) mContext

    .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo[] aNetworkInfos = cManager.getAllNetworkInfo();

    if (aNetworkInfos != null) {// 逐一查找状态为已连接的网络

    for (int i = 0; i < aNetworkInfos.length; i++) {

    bAvailable = aNetworkInfos[i].isAvailable();

    bConnected = aNetworkInfos[i].isConnected();

    strTypeName = aNetworkInfos[i].getTypeName();

    Log.d(TAG, "Available=" + bAvailable + " ; Connected="

    + bConnected + "; Type=" + strTypeName);

    }

    }

    NetworkInfo activeNetInfo = cManager.getActiveNetworkInfo();

    if (activeNetInfo != null) {

    Log.i(TAG, "ActiveNetwork Name=" + activeNetInfo.getTypeName()

    + " ; Type=" + activeNetInfo.getType() + " ; isAvailable="

    + activeNetInfo.isAvailable() + " ; Connected="

    + activeNetInfo.isConnected());

    strRes = "NET=ENABLE;";

    NetworkInfo networkInfo = cManager

    .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {

    strRes += "MOBLIE=OPNE;";

    } else {

    strRes += "MOBLIE=CLOSE;";

    }

    networkInfo = cManager

    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {

    Log.d(TAG, "ActiveNetwork " + networkInfo.getTypeName());

    strRes += "WIFI=OPNE;";

    } else {

    strRes += "WIFI=CLOSE;";

    }

    这时候如果有线网没有连接,则读取外置sdcard,获取wifi连接的地址

    1:判断外置sdcard是否存在

    public static final boolean hasSDCard() {

    return Environment.getExternalStorageState().equals(

    android.os.Environment.MEDIA_MOUNTED);

    }

    2:读取配置:

    if (Environment.getExternalStorageState().equals(

    Environment.MEDIA_MOUNTED)) {

    try {

    FileInputStream inputStream = new FileInputStream(

    path);

    byte[] b = new byte[inputStream.available()];

    inputStream.read(b);

    String result = "";

    result += new String(b);

    JSONObject object = new JSONObject(result);

    JSONObject jsonArray = new JSONObject(

    object.getString("dzstb"));

    String ssid = jsonArray.getString("wifi_ssid");

    String pwd = jsonArray.getString("wifi_pwd");

    3:连接指定wifi:

    public boolean Connect(String SSID, String Password, int iType,

    boolean bCloseOthers) {

    WifiConfiguration wifiConfig = CreateWifiInfo(SSID, Password, iType);

    if (wifiConfig == null) {

    Log.d(AppEnv.TAG, "WIFI Config = null");

    return false;

    }

    WifiConfiguration tmpConfig = IsExsits(SSID);

    if (tmpConfig != null) {

    Log.d(AppEnv.TAG, "WIFI Existed SSID and Remove " + tmpConfig.SSID);

    if (mWifiManager.removeNetwork(tmpConfig.networkId)) {

    Log.d(AppEnv.TAG, "WIFI Remove cfg OK");

    } else {

    Log.e(AppEnv.TAG, "WIFI Remove cfg fail");

    }

    }

    int netID = mWifiManager.addNetwork(wifiConfig);

    Log.d(AppEnv.TAG, "WIFI addNetwork netID = " + netID);

    // 连接netId所指的WIFI网络,并是其他的网络都被禁用

    boolean enabled = mWifiManager.enableNetwork(netID, bCloseOthers);

    Log.d(AppEnv.TAG, "WIFI enableNetwork = " + enabled);

    if (enabled) {

    boolean connected = mWifiManager.reconnect();

    Log.d(AppEnv.TAG, "WIFI reconnect = " + connected);

    }

    return enabled;

    }

    这里如果在长连接的通信状态,然后重新连接wifi后程序接收不到判断是否连接的状态,有点bug.

    4:判断设备优盘和外置SDCard挂载情况

    判断是否是优盘以及容量,这里会我们用shell命令来判断

    U盘获取判断代码:

    public String GetOtgResult(String strType, int num) {

    String strLine = "";

    String strTemp = "";

    String strRes = "";

    String resString = "";

    if (strType.equalsIgnoreCase("sda")) {

    int i = 0;

    // 8 0 12563968 sda

    FileReader file = null;

    BufferedReader buff = null;

    try {

    file = new FileReader("/proc/partitions");

    buff = new BufferedReader(file);

    while ((strLine = buff.readLine()) != null) {

    if (strLine.indexOf("sda") > 0) {

    i++;

    Log.d(TAG, strLine);

    strTemp = strLine;

    strTemp = (strTemp.trim()).replaceAll("\\s* |\t|\r|\n",

    ";");

    String[] columns = strTemp.split("\\;");

    if (columns[0].equals("")) {

    return "Res=FAIL:ERR=NO_SDA;";

    } else {

    strRes += "U" + i + "Total="

    + (Integer.parseInt(columns[2])) / 1024

    + "MB;";

    }

    }

    }

    buff.close();

    buff = null;

    file.close();

    file = null;

    } catch (IOException e) {

    e.printStackTrace();

    try {

    buff.close();

    } catch (IOException e1) {

    e1.printStackTrace();

    }

    buff = null;

    try {

    file.close();

    } catch (IOException e1) {

    e1.printStackTrace();

    }

    file = null;

    return "Res=FAIL;NUM=0;";

    }

    // 8 0 12563968 sda

    if (num <= i) {

    resString = "Res=PASS;" + "NUM=" + i + ";" + strRes;

    } else {

    resString = "Res=FAIL;" + "NUM=" + i + ";" + strRes;

    }

    }

    return resString;

    SDCard获取判断:

    public static String getAllStorage(Activity context, int count,

    boolean iswrite) {

    StorageManager storageManager = (StorageManager) context

    .getSystemService(Context.STORAGE_SERVICE);

    String strRes = "";

    if (storageManager == null) {

    Log.i(AppEnv.TAG, "Failed  to call StorageManager service");

    // return "Res=FAIL";

    }

    String[] volumePaths = null;

    Method methodGetVolumePaths = null;

    try {

    methodGetVolumePaths = storageManager.getClass().getMethod(

    "getVolumePaths");

    volumePaths = (String[]) methodGetVolumePaths

    .invoke(storageManager);

    } catch (NoSuchMethodException e) {

    e.printStackTrace();

    } catch (IllegalArgumentException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (IllegalAccessException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (InvocationTargetException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    if (null == volumePaths) {

    Log.e(AppEnv.TAG, "Failed to GetVolumePaths");

    return "Res=FAIL;NUM=0;TF1WR=FAIL;";

    }

    String name = "";

    DecimalFormat formatter = new DecimalFormat();

    formatter.setGroupingSize(3);

    int surecount = 0;

    for (int i = 0; i < volumePaths.length; i++) {

    if (volumePaths[i].startsWith("/mnt/extsd")) {

    surecount++;

    String path = volumePaths[i].toString();

    StatFs sfs = new StatFs(path);

    long blocSize = sfs.getBlockSize();

    long totalBlocks = sfs.getBlockCount();

    long availaBlock = sfs.getAvailableBlocks();

    long lTotal = (totalBlocks * (blocSize / 1024)) / 1024;

    long lAvail = (availaBlock * (blocSize / 1024)) / 1024;

    strRes += ";SDTotal" + surecount + "="

    + (Integer.parseInt(lTotal + "")) / 1024 + "GB;";

    Log.i(AppEnv.TAG, strRes);

    } else if (volumePaths[i].startsWith("/storage/emulated/0")) {

    surecount++;

    String path = volumePaths[i].toString();

    StatFs sfs = new StatFs(path);

    long blocSize = sfs.getBlockSize();

    long totalBlocks = sfs.getBlockCount();

    long availaBlock = sfs.getAvailableBlocks();

    long lTotal = (totalBlocks * (blocSize / 1024)) / 1024;

    long lAvail = (availaBlock * (blocSize / 1024)) / 1024;

    strRes += "SDTotal" + surecount + "="

    + (Integer.parseInt(lTotal + "")) / 1024 + "GB";

    }

    }

    if (count > surecount) {

    return "Res=FAIL;NUM=" + surecount + ";" + strRes;

    } else {

    return "Res=PASS;NUM=" + surecount + ";" + strRes;

    }

    }

    5:通过HDMI播放外置T卡的视频

    1:获取sdCard是否存在

    2:判断hdmi接口是否有插入状态:(返回1为插入)

    public static String getResultOfReadDev(String dev) {

    String strInfo = "";

    String info = "";

    String line = "";

    File file = new File(dev);

    if (!file.exists()) {

    return "Error: NO such file " + dev;

    }

    InputStream input = null;

    BufferedReader stdout = null;

    try {

    input = new FileInputStream(dev);

    } catch (FileNotFoundException e) {

    e.printStackTrace();

    return strInfo;

    }

    stdout = new BufferedReader(new InputStreamReader(input), 8 * 1024);

    try {

    while ((line = stdout.readLine()) != null) {

    info += line + "\n";

    }

    stdout.close();

    stdout = null;

    input.close();

    input = null;

    } catch (IOException e) {

    e.printStackTrace();

    if (stdout != null) {

    try {

    stdout.close();

    } catch (IOException e1) {

    e1.printStackTrace();

    }

    stdout = null;

    }

    if (input != null) {

    try {

    input.close();

    } catch (IOException e1) {

    e1.printStackTrace();

    }

    input = null;

    }

    }

    strInfo = info;

    return strInfo;

    }

    3:通过VideoView播放

    mVideo.setVideoURI(Uri.parse(getIntent().getStringExtra("filename")));//

    另外的开机启动配置,可以在刚开机的时候,就去判断是否有有限连接,如果没有的话,通过shell配置静态IP

    开机启动程序:

    <users-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">

    <receiver android:name=".Receiver">

    <intent-filter>

    <action android:name="android.intent.action.BOOT_COMPLETED"/>

    <category android:name="android.intent.category.HOME"/>

    </intent-filter>

    </receiver>


    public void onReceive(Context context, Intent intent) {

    // TODO Auto-generated method stub

    String strAction = intent.getAction();

    if (strAction.equals(Intent.ACTION_BOOT_COMPLETED)) {

    //进入主界面

    }

    shell配置有线网需要获取root权限,这里没有做处理,默认就是连接wifi进行通信.

    下面在补充下常用到的adb命令:

    反编译app:

    进入这个目录,d2j-dex2jar.bat  classes.dex 

    生成jar后用这个打开即可

    获取android界面并点击:

    adb shell input tap 400 200       400(横坐标)200(纵坐标)

    查看所有包名:

    adb shell pm list package

    push到内置sdcard:

    adb push 路径 /sdcard/

    判断某个界面是否在前台:

    相关文章

      网友评论

          本文标题:面向android设备之电视盒子

          本文链接:https://www.haomeiwen.com/subject/mewjottx.html