Overriding the hardware buttons on Android -
is possible override function of hardware button programmically on droid? specifically, i'd able override camera button on phone programmically. possible?
how handle camera button events
as camera button pressed broadcast message sent applications listening it. need make use of broadcast receivers , abortbroadcast() function.
1) create class extends broadcastreceiver , implement onreceive method.
the code inside onreceive method run whenever broadcast message received. in case have written program start activity called myapp.
whenever hardware camera button clicked default camera application launched system. may create conflict , block activity. e.g if creating own camera application may fail launch because default camera application using resources. there might other applications listening same broadcast. prevent call function "abortbroadcast()", this tell other programs responding broadcast.
public class hdc extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // todo auto-generated method stub // prevent other apps launching abortbroadcast(); // 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 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 added manifest file tell system program ready receive broadcast messages.
this line added receive intimation when hardware button clicked.
<action android:name="android.intent.action.camera_button" />
hdc class created in step 1(do not forget ".")
<receiver android:name=".hdc" >
the "abortbroadcast()" function called prevent other applications responding broadcast. if application last 1 receive message? prevent priority has set make sure app receives prior other program. set priority add line. current priority 10000 high, can change according requirements.
<intent-filter android:priority="10000">
Comments
Post a Comment