关于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);    }}

更多相关文章

  1. Android自定义控件——仿ios开关按钮
  2. Android(安卓)实现记住用户名和密码的功能
  3. android 自定义带关闭按钮的dialog
  4. Android(安卓)-- Button(按钮)的几种监听方式
  5. Android中原生WebView与HTML5 里的 JS交互
  6. Android(安卓)蓝牙开发基本流程
  7. 搭建Android开发平台(Android(安卓)studio)
  8. Android(安卓)Nine Patch图片及按钮背景
  9. Android中的Button自定义点击效果之改变点击时按钮的颜色

随机推荐

  1. 如何看待 Kotlin 成为 Android(安卓)官方
  2. 自动搜索私密信息与彻底删除APP--(一)清
  3. Flutter 学习之打包 - 纯Flutter项目生成
  4. Android学习——uses-sdk标签详解
  5. 如何用mysqldump进行全量和时间点备份
  6. mysql优化之like和=性能详析
  7. 详解MySQL 重做日志(redo log)与回滚日志(un
  8. MySQL 常用的拼接语句汇总
  9. MySQL Truncate用法详解
  10. MySQL20个高性能架构设计原则(值得收藏)