I set the priority
of my intent into 100000 but still my app is being ignored when I press the headset button. Instead, just trigger the Built-in
music player in my phone. What do I need to do to make this work?
Here`s my code
public class MainActivity extends Activity {
private Headset headset = new Headset();
public TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.TEXT);
Headset headset = new Headset();
IntentFilter mFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
mFilter.setPriority(1);
registerReceiver(headset, mFilter);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(headset);
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class Headset extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
String intAction = arg1.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intAction)){
return;
}
KeyEvent event = (KeyEvent)arg1.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if(event==null){
return;
}
int action = event.getAction();
if (action==KeyEvent.ACTION_DOWN){
txt.setText("you clicked me!!!");
}
abortBroadcast();
}
}
}