2010-08-31 26 views
1

我需要創建自定義廣播接收器,它會告訴我有關發生的任何PHONE_STATE事件。如何創建自定義廣播接收器

+1

什麼是你的問題? – CommonsWare 2010-08-31 08:57:35

+0

你想捕獲傳入的電話並打斷你的廣播嗎?還是有什麼你想要做的?你有用例依賴嗎? – 2010-08-31 09:04:45

+0

是的,你是對的。 當電話狀態改變,那麼我應該能夠中斷廣播,廣播接收者應該顯示適當的消息,如來電等..感謝您的評論 – 2010-08-31 09:50:01

回答

1

在你的清單,你應該定義如下:

<receiver 
     android:enabled="true" 
     android:label="@string/app_name" 
     android:name=".PhoneStateReceiver"> 
     <intent-filter> 
      <action 
       android:name="android.intent.action.PHONE_STATE" /> 
     </intent-filter> 
</receiver> 

樣品接收器類:

public class PhoneStateReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

    TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 

    switch(telManager.getCallState()) { 
    case TelephonyManager.CALL_STATE_IDLE: 
     //do something 
    } 
} 

}