Search code examples
androidandroid-4.4-kitkatremote-controlinfrared

Adding IR remote control to android application in android 4.4


i want my application to be controlled by infrared remote. and the app must run in kitkat version. basically here i am having 4 image views and i want the user to use IR remote to move and select a particular image. Can anyone help me on where to start? Should i stick only to a particular remote, or are the controls in remote same on all remotes.


Solution

  • At last found the solution!!!! :) Below is the sample code.

        public class DpadActivity extends Activity {
    
    private ImageView iv1,iv2;
    private static Toast tst;
    private int current_position=1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dpad);
        iv1=(ImageView)findViewById(R.id.imageView1);
        iv2=(ImageView)findViewById(R.id.ImageView2);
    
        iv1.requestFocus();
        iv1.setBackgroundResource(R.drawable.hilight);
    
    
        iv2.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "iv2 clicked", Toast.LENGTH_LONG).show();
            }
        });
    
    
        iv1.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "iv1 clicked", Toast.LENGTH_LONG).show();
            }
        });
    
    
    
    tst.makeText(getApplicationContext(), "Ha hai", Toast.LENGTH_LONG).show();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    
        if (KeyEvent.KEYCODE_DPAD_CENTER == keyCode
                || KeyEvent.KEYCODE_ENTER == keyCode) {
    tst.makeText(getApplicationContext(), "Enter Pressed", Toast.LENGTH_LONG).show();
            return true;
        } else if (KeyEvent.KEYCODE_DPAD_LEFT == keyCode) {
    tst.makeText(getApplicationContext(), "Left Pressed", Toast.LENGTH_LONG).show();
            return true;
        } else if (KeyEvent.KEYCODE_DPAD_RIGHT == keyCode) {
            tst.makeText(getApplicationContext(), "Right Pressed", Toast.LENGTH_LONG).show();
            return true;
        } else if (KeyEvent.KEYCODE_DPAD_UP == keyCode) {
            if(current_position==1)
            {
                iv2.requestFocus();
                current_position=2;
            }else if(current_position==2)
            {
                iv1.requestFocus();
                current_position=1;
            }
            tst.makeText(getApplicationContext(), "Up Pressed", Toast.LENGTH_LONG).show();
            return true;
        } else if (KeyEvent.KEYCODE_DPAD_DOWN == keyCode) {
            if(current_position==1)
            {
                iv2.requestFocus();
                current_position=2;
            }else if(current_position==2)
            {
                iv1.requestFocus();
                current_position=1;
            }
            tst.makeText(getApplicationContext(), "Down Pressed", Toast.LENGTH_LONG).show();
            return true;
    
        }
    
        Toast.makeText(getApplicationContext(), "Nothing Detected", Toast.LENGTH_LONG).show();
        return super.onKeyDown(keyCode, event);
    }
    }