覆盖在Android的硬件按钮 [英] Overriding the hardware buttons on Android

查看:115
本文介绍了覆盖在Android的硬件按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能覆盖一个硬件按钮的功能programmically上一个机器人?具体来说,我希望能够覆盖我的手机上的拍照按钮programmically。这可能吗?

Is it possible to override the function of a hardware button programmically on a droid? Specifically, I'd like to be able to override the camera button on my phone programmically. Is this possible?

推荐答案

一旦相机按钮pressed的广播消息发送到听着它的所有应用程序。你需要利用广播接收器和abortBroadcast()函数。

How to Handle Camera Button Events

As soon as camera button is pressed a broadcast message is sent to all the applications listening to it. You need to make use of Broadcast receivers and abortBroadcast() function.

1)创建一个扩展广播接收器一类,并实现的onReceive方法。

1) Create a class that extends BroadcastReceiver and implement onReceive method.

的onReceive方法内的code每当接收广播消息将运行。在这种情况下,我写了一个程序来启动名为​​myApp的活动。

The code inside onReceive method will run whenever a broadcast message is received. In this case I have written a program to start an activity called myApp.

每当硬件相机按钮被点击默认摄像头应用程序是由系统启动。这可能会造成冲突,阻止您的活动。例如如果您正在创建自己的摄像头应用程序可能无法启动,因为默认摄像机应用程序将使用的所有资源。此外有可能是其中正在收听的相同广播的其他应用程序。以prevent这个调用函数abortBroadcast(),这将告诉你应对这个广播的其他程序。

Whenever hardware camera button is clicked the default camera application is launched by the system. This may create a conflict and block your activity. E.g If you are creating your own camera application it may fail to launch because default camera application will be using all the resources. Moreover there might be other applications which are listening to the same broadcast. To prevent this call the function "abortBroadcast()", this will tell other programs that you are responding to this broadcast.

public class HDC extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
// Prevent other apps from launching
     abortBroadcast();
// Your Program
     Intent startActivity = new Intent();
        startActivity.setClass(context, myApp.class);
        startActivity.setAction(myApp.class.getName());
        startActivity.setFlags(
        Intent.FLAG_ACTIVITY_NEW_TASK
        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        context.startActivity(startActivity);
        }
    }
}

2) Add below lines to your android manifest file.

<receiver android:name=".HDC" >
    <intent-filter android:priority="10000">         
        <action android:name="android.intent.action.CAMERA_BUTTON" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>            
</receiver>

以上行添加到您的清单文件告诉系统,你的计划是准备接收广播消息。

The above lines are added to your manifest file to tell the system that your program is ready to receive broadcast messages.

这行被添加到接收暗示被点击硬件按钮时。

This line is added to receive an intimation when hardware button is clicked.

<action android:name="android.intent.action.CAMERA_BUTTON" />

HDC是在步骤1中创建的类(不要忘了。)

HDC is the class created in step 1(do not forget the ".")

<receiver android:name=".HDC" >

的abortBroadcast()功能被从响应所述广播调用以prevent其它应用。如果你的应用是什么最后一个收到消息?以prevent一些这方面的优先级必须设置,以确保您的应用程序接收到任何其他程序它之前。要设置优先加入这一行。目前的当务之急是10000,这是非常高的,你可以根据你的要求改变它。

The "abortBroadcast()" function is called to prevent other applications from responding to the broadcast. What if your application is the last one to receive the message? To prevent this some priority has to be set to make sure that your app receives it prior to any other program. To set priority add this line. Current priority is 10000 which is very high, you can change it according to your requirements.

<intent-filter android:priority="10000">

这篇关于覆盖在Android的硬件按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆