您好,欢迎来到源码搜藏!分享精神,快乐你我!提示:担心找不到本站?在百度搜索“源码搜藏”,网址永远不丢失!
  • 首 页
  • 在线工具
  • 当前位置:首页 > 安卓源码 > 技术博客 >

    Android TV机顶盒开发之初级接触

    时间:2016-12-16 22:43 来源:互联网 作者:源码搜藏 浏览:收藏 挑错 推荐 打印

    最近接触了点Andiroid 机顶盒开发,简单的写一下,希望我的经验可以给各位带来一点经验。图片我就不上传的,很麻烦的! 首先是Android的apk如何安装到机顶盒上? 将TV连接机顶盒,然后找到设置,连接的网络,查看网络的IP地址,例如(192.168.0.1); 打开And

    最近接触了点Andiroid 机顶盒开发,简单的写一下,希望我的经验可以给各位带来一点经验。图片我就不上传的,很麻烦的!
    首先是Android的apk如何安装到机顶盒上?

    1. 将TV连接机顶盒,然后找到设置,连接的网络,查看网络的IP地址,例如(192.168.0.1);

    2. 打开AndroidStudio的Terminal下命令:adb connect 192.168.0.1,然后等待电视上出现是否允许调试的字样,点击允许。

    3. 然后Terminal会出现带...successful...的字样,表示连接成功,你也可以通过点击Run App(没错,就是AndroidStudio上我们经常运行的绿三角的按钮)查看,会出现你的盒子的名字代表连接成功。

    4. 当你写完程序,想调试的话可以直接点击绿三角按钮,选择盒子运行,直接安装在盒子上了。

    5. 如果你想将apk安装到盒子上,可以把apk放到该项目下,然后通过下adb install apk名字.apk 进行安装,然后卸载为adb uninstall 名字.apk

    6. 如果你想断开与盒子的连接,可以通过下 adb kill-server杀死adb来断开,也可以通过 adb start-server来重新连接。(我这里不能两台电脑同时连接盒子,必须其中一个断开连接,另一个才能连)
      接下来我简单说一下app是如何监听遥控器的键的,首先遥控器上的上下左右是不用监听过的,点击上下左右是可以自动跳到你的控件上去的(但但是你的控件必须加上 android:focusable="true" 这个属性)我这里监听的是TextView的点击(我这里发现遥控器按下Button是无法禁监听到,如果有大牛知道遥控器如何实现Button的点击事件,请告知小弟)

    重写OnKeyDown(),然后判断该TextVIew是否获取焦点,如果获取到,就响应对应的事件。
    布局:布局没什么,主要是加上android:focusable="true"这个属性

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:background="@mipmap/bg"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.yaodan.tvdemo.MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <TextView
                android:background="@drawable/title_textselect"
                android:textColor="@color/textcolorselct"
                android:id="@+id/bt_a"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:text="按钮a" />
    
            <TextView
                android:background="@drawable/title_textselect"
                android:textColor="@color/textcolorselct"
                android:id="@+id/bt_b"
                android:focusable="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:layout_height="wrap_content"
                android:text="按钮b" />
    
    
        </LinearLayout>
    
        <LinearLayout
    
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <TextView
                android:background="@drawable/title_textselect"
                android:textColor="@color/textcolorselct"
                android:id="@+id/bt_c"
                android:focusable="true"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:layout_height="wrap_content"
                android:text="按钮c" />
    
            <TextView
                android:background="@drawable/title_textselect"
                android:textColor="@color/textcolorselct"
                android:id="@+id/bt_d"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:text="按钮d" />
        </LinearLayout>
    
    </LinearLayout>
    

    实现代码

    package com.yaodan.tvdemo;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.yaodan.tvdemo.adda.AddActivityA;
    import com.yaodan.tvdemo.addb.AddActivityB;
    import com.yaodan.tvdemo.addc.AddActivityC;
    import com.yaodan.tvdemo.addd.AddActivityD;
    
    public class MainActivity extends AppCompatActivity {
        private TextView bta, btb, btc, btd;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        public void initView() {
            bta = (TextView) findViewById(R.id.bt_a);
            btb = (TextView) findViewById(R.id.bt_b);
            btc = (TextView) findViewById(R.id.bt_c);
            btd = (TextView) findViewById(R.id.bt_d);
        }
    //重写按键方法
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {            Toast.makeText(MainActivity.this,"bbb",Toast.LENGTH_LONG).show();
            //判断bta是否获取焦点
                if (bta.hasFocus()) {
                    Toast.makeText(MainActivity.this,"按钮a被点击了",Toast.LENGTH_LONG).show();
                } else if (btb.hasFocus()) {
                    Toast.makeText(MainActivity.this,"按钮b被点击了",Toast.LENGTH_LONG).show();              
                } else if (btc.hasFocus()) {
                    Toast.makeText(MainActivity.this,"按钮c被点击了",Toast.LENGTH_LONG).show();               
                } else if (btd.hasFocus()) {
                    Toast.makeText(MainActivity.this,"按钮d被点击了",Toast.LENGTH_LONG).show();              
                }
            }
            return super.onKeyDown(keyCode, event);
        }
        
    }
    Android TV机顶盒开发之初级接触转载http://www.codesocang.com/anzhuoyuanma/boke/34001.html
    标签:网站源码