我已經看到了幾個如何實現BroadCastReceiver的例子,但我應該如何實現一個服務,必須對一些待處理的Intent(例如來電)作出反應...... 其實我很想知道但是在一個活動中。但是在一個活動中,你顯然有一個擴展服務或活動的類,所以它不能擴展BroadCastReceiver ... 看起來我們不能製作「平臺感知」服務和/或工作之外的活動?服務和BroadCastReceiver
9
A
回答
12
要註冊的活動收到了一定的意圖您需要:
// Flag if receiver is registered
private boolean mReceiversRegistered = false;
// I think this is the broadcast you need for something like an incoming call
private String INCOMING_CALL_ACTION = "android.intent.action.PHONE_STATE";
// Define a handler and a broadcast receiver
private final Handler mHandler = new Handler();
private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Handle reciever
String mAction = intent.getAction();
if(mAction.equals(INCOMING_CALL_ACTION) {
// Do your thing
}
}
@Override
protected void onResume() {
super.onResume();
// Register Sync Recievers
IntentFilter intentToReceiveFilter = new IntentFilter();
intentToReceiveFilter.addAction(INCOMING_CALL_ACTION);
this.registerReceiver(mIntentReceiver, intentToReceiveFilter, null, mHandler);
mReceiversRegistered = true;
}
@Override
public void onPause() {
super.onPause();
// Make sure you unregister your receivers when you pause your activity
if(mReceiversRegistered) {
unregisterReceiver(mIntentReceiver);
mReceiversRegistered = false;
}
}
然後你還需要一個意圖過濾器添加到您的清單:
<activity android:name=".MyActivity" android:label="@string/name" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</activity>
4
您可以創建一個內部類
class A extends Activity {
BroadcastReceiver r = new BroadcastReceiver(){
// code to handle broadcase
}
}
該類會收到事件,你可以傳遞到主處理器,或者只是調用一些外部方法
+0
它是否需要成爲內部類? – n611x007 2013-10-30 11:47:04
4
其實你可以通過添加監聽器TelephonyManager
你在你的服務定義PhoneStateListener /活動,以來電反應
private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};
然後在onCreate方法:
mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
最後你清除onDestroy中的監聽器:
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
在這種情況下非常簡單。
相關問題
- 1. 藍牙服務器和BroadCastReceiver
- 2. BroadcastReceiver與服務
- 3. Android BroadcastReceiver與服務
- 4. 註冊broadcastReceiver服務
- 5. Android服務+ BroadcastReceiver,PhoneStateListener
- 6. 綁定服務BroadcastReceiver
- 7. 幫助使用appwidget與broadcastreceiver和服務?
- 8. AlarmManager,BroadcastReceiver和服務不工作
- 9. 在BroadcastReceiver中同時停止BroadcastReceiver和服務
- 10. BroadcastReceiver->服務 - >通知
- 11. 使用BroadCastReceiver實現服務
- 12. 實現BroadcastReceiver即服務 - Android
- 13. 服務,IntentService,BroadcastReceiver或AlarmManager?
- 14. 從BroadcastReceiver啓動服務
- 15. 從BroadcastReceiver啓動服務
- 16. 從BroadcastReceiver驅動服務API
- 17. 正在服務的BroadcastReceiver
- 18. BroadcastReceiver在服務沒有反應
- 19. 發送一個BroadcastReceiver意圖服務
- 20. 服務不從BroadcastReceiver運行? - Android
- 21. 無法從BroadcastReceiver中終止服務
- 22. 從BroadcastReceiver調用後臺服務
- 23. 如何從broadcastReceiver激活服務?
- 24. BroadcastReceiver一段時間後啓動服務
- 25. 來自broadcastReceiver的服務呼叫功能
- 26. 從BroadcastReceiver開始從事服務活動
- 27. 內部創建BroadcastReceiver時發出服務
- 28. 結合本地服務從一個BroadcastReceiver
- 29. 來自BroadcastReceiver的Android調用服務
- 30. Android BroadcastReceiver等待服務啓動或將WakeLock傳輸到服務?
如果您創建了BroadcastReceiver的新實例,然後在您的代碼中註冊它,提供了一個意圖過濾器對象,則不需要在清單文件的活動定義中創建。 –
hara
2010-06-02 16:45:20