2012-07-30 28 views
7

嗨,我正在處理一個應用程序,當耳機從手機中移除時會生成一個事件。 我已經創建與接收方法的廣播接收器作爲BroadcastReceiver用於拔掉耳機

public void onReceive(Context context, Intent intent) { 

     // TODO Auto-generated method stub 
     String action = intent.getAction(); 
     Log.i("Broadcast Receiver", "Hello"); 
     if((action.compareTo(Intent.ACTION_HEADSET_PLUG)) == 0) //if the action match a headset one 
     { 
      int headSetState = intent.getIntExtra("state", 0);  //get the headset state property 
      int hasMicrophone = intent.getIntExtra("microphone", 0);//get the headset microphone property 
      if((headSetState == 0) && (hasMicrophone == 0))  //headset was unplugged & has no microphone 
      { 

        //do whatever 
      } 
     }   

    } 

調用此方法如下

IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); 
     HeadSetBroadCastReceiver receiver = new HeadSetBroadCastReceiver(); 
     registerReceiver(receiver, receiverFilter); 

也我有清單中註冊此作爲

<receiver android:name=".HeadsetBroadCastReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.ACTION_HEADSET_PLUG"/> 
    </intent-filter> 
</receiver> 

和權限

但是這並不奏效可以你通過這個指導我嗎?

+0

共同的傢伙沒人能指導我? – Sumit 2012-07-30 06:47:50

+1

[選中此項](http:// stackoverflow。問題/ 4092438/intent-action-headset-plug-is-received-when-activity-starts) – 2012-07-30 07:11:36

+0

[This works](http://stackoverflow.com/a/6366238/726863) – 2012-07-30 07:23:36

回答

0

我建議您在onReceive內部切換斷點,並在Debug模式下檢查您插入手機設備時收到的事件類型。 然後插入此事件而不是Intent.ACTION_HEADSET_PLUG 謝謝。

+0

我認爲廣播接收器沒有獲得註冊。:( – Sumit 2012-07-30 07:18:33

0

我想在註冊廣播接收機時有個問題。請參閱此post並查看它是否可行。

異常在我的logcat

07-30 12:41:01.448: E/ActivityThread(321): Activity com.example.test.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()? 
07-30 12:41:01.448: E/ActivityThread(321): android.app.IntentReceiverLeaked: Activity com.example.test.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()? 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher.<init>(ActivityThread.java:939) 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.ActivityThread$PackageInfo.getReceiverDispatcher(ActivityThread.java:734) 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:791) 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.ContextImpl.registerReceiver(ContextImpl.java:778) 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.ContextImpl.registerReceiver(ContextImpl.java:772) 
07-30 12:41:01.448: E/ActivityThread(321): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318) 
07-30 12:41:01.448: E/ActivityThread(321): at com.example.test.MainActivity.onCreate(MainActivity.java:19) 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
07-30 12:41:01.448: E/ActivityThread(321): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-30 12:41:01.448: E/ActivityThread(321): at android.os.Looper.loop(Looper.java:123) 
07-30 12:41:01.448: E/ActivityThread(321): at android.app.ActivityThread.main(ActivityThread.java:4627) 
07-30 12:41:01.448: E/ActivityThread(321): at java.lang.reflect.Method.invokeNative(Native Method) 
07-30 12:41:01.448: E/ActivityThread(321): at java.lang.reflect.Method.invoke(Method.java:521) 
07-30 12:41:01.448: E/ActivityThread(321): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
07-30 12:41:01.448: E/ActivityThread(321): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
07-30 12:41:01.448: E/ActivityThread(321): at dalvik.system.NativeStart.main(Native Method) 

這是我註冊的廣播接收器在我的項目之一:

的onCreate:

$////////////////broadcast reciever///////////// 
    IntentFilter iFilter = new IntentFilter(); 
    iFilter.addAction(handler.ACTION); 
    iFilter.addCategory(Intent.CATEGORY_DEFAULT); 
    handler = new HandleTime(); 
    registerReceiver(handler, iFilter); 

的Manifest.xml:

<activity android:name=".MainActivity" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

    </activity> 
1

回答我的問題是這樣的..多謝回覆每一個人在這裏

class NoisyAudioStreamReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) { 
      // pause(); 
      Log.d("Mesage","Unplug"); 
      Toast.makeText(context, "Heello",Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY); 
     NoisyAudioStreamReceiver receiver = new NoisyAudioStreamReceiver(); 
     registerReceiver(receiver, intentFilter); 

16

它的棘手問題,但你可以使用廣播作爲下面你活動

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    myReceiver = new HeadSetReceiver(); 
} 

的onResume()跟我 運作良好方法註冊您的廣播

public void onResume() { 
    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG); 
    registerReceiver(myReceiver, filter); 
    super.onResume(); 
} 

然後在您的活動中聲明您的廣播

private class HeadSetReceiver extends BroadcastReceiver { 
    @Override public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) { 
      int state = intent.getIntExtra("state", -1); 
      switch (state) { 
      case 0: 
       Log.d(TAG, "Headset unplugged"); 
       break; 
      case 1: 
       Log.d(TAG, "Headset plugged"); 
       break; 
      } 
     } 
    } 
} 

希望它的幫助,,,

相關問題