因为我一个小伙伴一起搞一个项目,他负责硬件我负责android。然后要在手机上实现红外线遥控的功能。在网上找了很多资料终于集众家所长,搞定了。

package com.example.zmx.myapplication;import android.annotation.TargetApi;import android.os.Build;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.hardware.ConsumerIrManager;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends ActionBarActivity {    private ConsumerIrManager IR;    private TextView show;    private Button btn_open;    private Button btn_close;    private Button btn_shake;    private Button btn_shake_off;    boolean IRBack;    //各种功能的编码段    int[] pattern = new int[] {  //0x45 开            9000,4500,            560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,            560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,            560,1690,    560,565,   560,1690,    560,565,    560,565,    560,565,   560,1690,   560,565,            560,565,   560,1690,    560,565,   565,1690,   565,1690,   560,1690,    560,565,    560,1690,            9000,2250,  2250,94000, 9000,2250,  2250,94000};    int[] pattern1 = new int[] {  //0x46 关            9000,4500,            560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,            560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,            560,565,    560,1690,   560,1690,    560,565,    560,565,    560,565,   560,1690,   560,565,            560,1690,   560,565,    560,565,   565,1690,   565,1690,   560,1690,    560,565,    560,1690,            9000,2250,  2250,94000, 9000,2250,  2250,94000};    int[] pattern2 = new int[] {  //0x44 启动摇头            9000,4500,            560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,            560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,            560,565,    560,565,   560,1690,    560,565,    560,565,    560,565,   560,1690,   560,565,            560,1690,   560,1690,    560,565,   565,1690,   565,1690,   560,1690,    560,565,    560,1690,            9000,2250,  2250,94000, 9000,2250,  2250,94000};    int[] pattern3 = new int[] {  //0x40 关闭摇头            9000,4500,            560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,    560,565,            560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,   560,1690,            560,565,    560,565,    560,565,    560,565,    560,565,    560,565,   560,1690,   560,565,            560,1690,   560,1690,   560,1690,   565,1690,   565,1690,   560,1690,    560,565,    560,1690,            9000,2250,  2250,94000, 9000,2250,  2250,94000};    @Override    protected void onCreate(Bundle savedInstanceState) {        IR=(ConsumerIrManager)getSystemService(CONSUMER_IR_SERVICE);//需要使用ConsumerIrManager类实现红外线的发送        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        inItEvent();        initViewsAndEvents();        btn_open.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                if (IRBack) {                    sendMsg(37900, pattern);//其中的pattern是遥控器所要实现的功能码                    show.setText("开机");                } else {                    show.setText("对不起没有红外功能");                }            }        });        btn_close.setOnClickListener(new View.OnClickListener(){            public void onClick(View v){                if (IRBack) {                    sendMsg(37900, pattern1);                    show.setText("关了");                } else {                    show.setText("对不起没有红外功能");                }            }        });        btn_shake.setOnClickListener(new View.OnClickListener(){            public void onClick(View v){                if (IRBack) {                    sendMsg(37900, pattern2);                    show.setText("摇头了");                } else {                    show.setText("对不起没有红外功能");                }            }        });        btn_shake_off.setOnClickListener(new View.OnClickListener(){            public void onClick(View v){                if (IRBack) {                    sendMsg(37900, pattern3);                    show.setText("不摇头");                } else {                    show.setText("对不起没有红外功能");                }            }        });    }//判断手机是否有红外线功能    private void inItEvent() {        IR = (ConsumerIrManager) getSystemService(CONSUMER_IR_SERVICE);        //???sdk?汾????4.4?????????к?????????????android?汾??        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            IRBack = IR.hasIrEmitter();            if (!IRBack) {                Toast.makeText(getApplicationContext(), "没有红外功能", Toast.LENGTH_SHORT).show();            } else {                Toast.makeText(getApplicationContext(), "有红外功能", Toast.LENGTH_SHORT).show();            }        }    }    @TargetApi(Build.VERSION_CODES.KITKAT)    private void sendMsg(int carrierFrequency, int[] pattern) {        IR.transmit(carrierFrequency, pattern);        Toast.makeText(getApplicationContext(), "开了", Toast.LENGTH_SHORT).show();        String content = null;        for(int i = 0;i

要注意在AndroidManifest.xml中还要引入权限

更多相关文章

  1. java.lang.ClassCastException:org.apache.harmony.luni.interna
  2. Android(安卓)关于倒计时功能的实现
  3. Android软件盘(EditText)的搜索功能
  4. android 关于 ScrollView嵌套GridView,ListView 显示和滑动的问题
  5. Android(安卓)通讯录学习笔记之——目标:调用系统通讯录的编辑功
  6. Android(安卓)xml 文件代码自动提示
  7. Android判断定位功能是否可用
  8. 【Android】获取手机通讯录中的联系人信息
  9. android 使用Monkey进行压力测试

随机推荐

  1. Activity学习日记(一)
  2. windows环境下进入到android 模拟器
  3. Android O: 触摸事件传递流程源码分析(上
  4. 探究Android之ClassLoader
  5. 收藏各种技术源码
  6. Android(安卓)应用程序的组成部分
  7. postInvalidate(),invalidate(),requestLay
  8. 下拉选择菜单Spinner的使用
  9. Android中改变Activity的不同icon:activit
  10. Delphi XE5 for Android (十一)