Android下如何使用硬键盘快捷键。

我们在Windows下常常有一些快捷键来启动某个应用的需求,同样在我们的Android系统下也可以实现这样的作用。

比如按下CAMERA键,来启动Camera应用。

使用步骤如下,

  1 先定义好CAMEAR键值,比如KEYCODE_CAMERA=27并要在xxx_Keypad.kl定义好扫描码与CAMERA对应的关系

 如 key 212   CAMERA

  2 定义相关BROADCAST_INTENT_ACTION @Intent.java

  @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String ACTION_CAMERA_BUTTON = "android.intent.action.CAMERA_BUTTON";
  

3 在系统文件PhoneWindow.java中有onKeyDown()函数来捕获CAMERA键值并发送相关intent.

  case KeyEvent.KEYCODE_CAMERA: {

   ....

 // Broadcast an intent that the Camera button was longpressed
                    Intent intent = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
                    intent.putExtra(Intent.EXTRA_KEY_EVENT, event);
                    getContext().sendOrderedBroadcast(intent, null);

       }

4 在camera应用下如果要接收这个Broadcast消息,需要在AndroidManifest.xml中增加receive 相关的intent-filter.

  <receiver android:name="CameraButtonIntentReceiver">
   <intent-filter>
    <action android:name="android.intent.action.CAMERA_BUTTON" />
   </intent-filter>
  </receiver>

 

5 定义类CameraButtonIntentReceiver 并重载onReceive方法来启动CameraActivity

public class CameraButtonIntentReceiver extends BroadcastReceiver {
    public CameraButtonIntentReceiver() {
    }
    
     @Override
    public void onReceive(Context context, Intent intent) {
        // Try to get the camera hardware
        CameraHolder holder = CameraHolder.instance();
        if (holder.tryOpen() == null) return;

        // We are going to launch the camera, so hold the camera for later use
        holder.keep();
        holder.release();
        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setClass(context, Camera.class);
        i.addCategory("android.intent.category.LAUNCHER");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
   }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值