android


how to receive intent from headset button


The simple question is how to receive intent from headset button? I have searched for a while and nothing worked out.
My app aims on API 15-25
in Manifest
<activity
android:name=".Home"
android:windowSoftInputMode="stateAlwaysHidden"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action
android:name="com.example.action.PAUSE_PLAY_INTENT_FILTER" />
//this should be unique string as action
<action
android:name="com.example.action.NEXT_TRACK_INTENT_FILTER" />
//this should be unique string as action
<action
android:name="com.example.action.PREVIOUS_TRACK_INTENT_FILTER"
/>
//this should be unique string as action
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
<action
android:name="com.example.action.UPDATE_ALL_LISTS_IN_APP" />
//this should be unique string as action/>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</activity>
in onCreate in Activity
IntentFilter mediaIntent = new IntentFilter();
mediaIntent.addAction(Intent.ACTION_MEDIA_BUTTON);
mediaIntent.setPriority(999999999);
registerReceiver(mMemoryStorage.mBroadcastReceiver, mediaIntent);
and in onReceive in BroadcastReceiver
String intentAction = intent.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)){
KeyEvent event = (KeyEvent) intent
.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
int keycode = event.getKeyCode();
int action = event.getAction();
Log.i("keycode", String.valueOf(keycode));
Log.i("action", String.valueOf(action));
//onKeyDown(keyCode, event)
if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
|| keycode == KeyEvent.KEYCODE_HEADSETHOOK&&action == KeyEvent.ACTION_DOWN) {
if(mMemoryStorage!=null){
System.out.println("playpause");
if(mMemoryStorage.musicSrv.isPng()){
mMemoryStorage.musicSrv.pausePlayer();
mMemoryStorage.musicSrv.updateNotification();
} else if(mMemoryStorage.musicSrv.isPaused){
mMemoryStorage.musicSrv.playPlayer();
mMemoryStorage.musicSrv.updateNotification();
}
//update seekbar playback in PlaySongFragment
Message message = new Message();
message.what = Params.UPDATE_SEEKBAR_PLAYSONGFRAGMENT;
mMemoryStorage.mHandler.sendMessage(message);
}
}

Related Links

How to create a right to left navigation drawer in android
Not able to Instantiate the android driver, If the Appium server is started from the code
Is there a way to create Proguard settings for my Android Application?
Internet connection checking (Android & iOS) cocos2dx-V3.6
i Want to do this way to customized Share Intent using Android
Null pointer when calling a method in a child class
Why am I getting some paddingLeft kind of gap in LinearLayout despite of setting its padding to 0?
What is ART and DART in Android
ContentResolver Client App cannot find table from ContentProvider
Android LocationManager GPS_PROVIDER time is different from NETWORK_PROVIDER time
Clean up Parse data after app uninstall
ANDROID_HOME Path Set But Not Being Read
Android Facebook Story post return message: Invalid parameter
Android external Service from a library (AAR, not Shared Service)
No cross/minimize button on AVD emulator
startLeScan returns false

Categories

HOME
uml
scikit-learn
gdb
apache-flink
fpga
pda
pyqt
youtube-data-api-v3
apache-spark-mllib
can
pdfbox
javacc
entity-framework-core
jetty
gluon-mobile
elasticsearch-5
extjs4.2
endeca
android-securityexception
alamofire
shared-libraries
gource
gruntfile
aws-sdk
firefox-addon
video-editing
soci
symbols
dataflow-diagram
pchart
displaytag
resourcebundle
jquery-callback
windows-server-2003
xcode-ui-testing
flooding
oculus
progress-bar
datastax-enterprise-graph
errbot
iis-express
boost-asio
aws-codecommit
mobx
kudu
quickfixj
memory-address
clickjacking
openwhisk
pdw-file-browser
jwplayer6
alphabetical
lint
amazon-ses
google-data-api
tasm
xv6
smoothstate.js
signal-strength
apache-directory
opennms
corruption
android-testing
ngmaterial
flac
pocketsphinx-android
juice-ui
testrail
meteor-packages
async.js
iotivity
mta
jquery-jscrollpane
observablecollection
respondcms
qiime
highland.js
vigenere
google-books
pax-exam
slash
zscript
teamcity-9.1
cross-join
dbaccess
big-ip
contrast
metatrader5
encryption-asymmetric
template-deduction
lnk
matrix-inverse
dct
ios8-extension
qscrollarea
http-patch
jeromq
scmmanager
alice
asplinkbutton
bjyauthorize
midlet
nokia-s40
monotouch.dialog
cuda-gdb
lambdaj
jquery-tools
automationelement
webdev.webserver
uipagecontrol
xui
metaweblog
maven-1
zend-rest
zen
tabpanel
scrollbars
glui
cuda.net
product-management

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App