download: http://www.mediafire.com/download/0667cfia4yo0yig/HeadsetControll.zip
package com.headset;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
public class AudioEqualizerActivity extends Activity {
// Using LinearLayout instead of R.layout.main (main.xml)
BroadcastReceiver broadcastsHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);// "android.intent.action.MEDIA_BUTTON"
MediaButtonReceiver r = new MediaButtonReceiver();
filter.setPriority(100);
registerReceiver(r, filter);
// unregisterReceiver(broadcastsHandler);
Log.e("----onCreate--", " ");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
broadcastsHandler = new MediaButtonReceiver();
registerReceiver(broadcastsHandler, new IntentFilter(
Intent.ACTION_HEADSET_PLUG));
registerMediaButton(AudioEqualizerActivity.this);
}
public static void registerMediaButton(Context context) {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(context.getPackageName(),
MediaButtonReceiver.class.getName());
audioManager.registerMediaButtonEventReceiver(receiver);
}
/**
* Unregister the media buttons from AudioManager.
*
* @param context
* A context to use.
*/
public static void unregisterMediaButton(Context context) {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(context.getPackageName(),
MediaButtonReceiver.class.getName());
audioManager.unregisterMediaButtonEventReceiver(receiver);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(broadcastsHandler);
unregisterMediaButton(AudioEqualizerActivity.this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(broadcastsHandler);
unregisterMediaButton(AudioEqualizerActivity.this);
}
}
package com.headset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MediaButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("----onReceive--", " ");
if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
Log.e("----jack out--", " ");
if (intent.getExtras().getInt("state") == 1)// if plugged
Toast.makeText(context, "earphones plugged", Toast.LENGTH_LONG)
.show();
else
Toast.makeText(context, "earphones un-plugged",
Toast.LENGTH_LONG).show();
}
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
Toast.makeText(context, "button pressed", Toast.LENGTH_LONG).show();
// key=intent.getExtras().getString("EXTRA_KEY_EVENT");
}
abortBroadcast();
}
}
mainifest
------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.headset"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- This is needed for isWiredHeadsetOn() to work in some cases. (bug?) -->
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AudioEqualizerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.headset.MediaButtonReceiver" >
<intent-filter android:priority="10" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
<intent-filter android:priority="20" >
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>
</application>
</manifest>
package com.headset;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
public class AudioEqualizerActivity extends Activity {
// Using LinearLayout instead of R.layout.main (main.xml)
BroadcastReceiver broadcastsHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);// "android.intent.action.MEDIA_BUTTON"
MediaButtonReceiver r = new MediaButtonReceiver();
filter.setPriority(100);
registerReceiver(r, filter);
// unregisterReceiver(broadcastsHandler);
Log.e("----onCreate--", " ");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
broadcastsHandler = new MediaButtonReceiver();
registerReceiver(broadcastsHandler, new IntentFilter(
Intent.ACTION_HEADSET_PLUG));
registerMediaButton(AudioEqualizerActivity.this);
}
public static void registerMediaButton(Context context) {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(context.getPackageName(),
MediaButtonReceiver.class.getName());
audioManager.registerMediaButtonEventReceiver(receiver);
}
/**
* Unregister the media buttons from AudioManager.
*
* @param context
* A context to use.
*/
public static void unregisterMediaButton(Context context) {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
ComponentName receiver = new ComponentName(context.getPackageName(),
MediaButtonReceiver.class.getName());
audioManager.unregisterMediaButtonEventReceiver(receiver);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(broadcastsHandler);
unregisterMediaButton(AudioEqualizerActivity.this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(broadcastsHandler);
unregisterMediaButton(AudioEqualizerActivity.this);
}
}
package com.headset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MediaButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("----onReceive--", " ");
if (Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
Log.e("----jack out--", " ");
if (intent.getExtras().getInt("state") == 1)// if plugged
Toast.makeText(context, "earphones plugged", Toast.LENGTH_LONG)
.show();
else
Toast.makeText(context, "earphones un-plugged",
Toast.LENGTH_LONG).show();
}
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
Toast.makeText(context, "button pressed", Toast.LENGTH_LONG).show();
// key=intent.getExtras().getString("EXTRA_KEY_EVENT");
}
abortBroadcast();
}
}
mainifest
------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.headset"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- This is needed for isWiredHeadsetOn() to work in some cases. (bug?) -->
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AudioEqualizerActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.headset.MediaButtonReceiver" >
<intent-filter android:priority="10" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
<intent-filter android:priority="20" >
<action android:name="android.intent.action.HEADSET_PLUG" />
</intent-filter>
</receiver>
</application>
</manifest>
Does this code work?
ReplyDelete