Android 服务和广播接收器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2958800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Service and a BroadCastReceiver
提问by TiGer
I have seen several examples of how to implement a BroadCastReceiver, but how should I implement a Service that has to react to some pending Intent (for example incoming phone call)... Actually I was wondering about the same "problem" but in an Activity.. You obviously have a class which extends a Service or an Activity) so it cannot also extend BroadCastReceiver... It looks like we cannot make "platform-aware" services and/or Activties?
我已经看到了几个关于如何实现 BroadCastReceiver 的例子,但是我应该如何实现一个必须对某些挂起的 Intent(例如来电)做出反应的服务......实际上我想知道同样的“问题”,但在一个Activity..你显然有一个扩展服务或活动的类)所以它也不能扩展BroadCastReceiver......看起来我们不能制作“平台感知”服务和/或活动?
回答by m6tt
To register an activity to receive a certain intent you need to:
要注册活动以接收特定意图,您需要:
// 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;
}
}
Then you will also need to add an intent-filter to your manifest:
然后你还需要在你的清单中添加一个意图过滤器:
<activity android:name=".MyActivity" android:label="@string/name" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</activity>
回答by reflog
you can create an inner class
你可以创建一个内部类
class A extends Activity {
BroadcastReceiver r = new BroadcastReceiver(){
// code to handle broadcase
}
}
that class will receive events, which you can pass to main handler, or just call some outer methods
该类将接收事件,您可以将其传递给主处理程序,或者只是调用一些外部方法
回答by Witold Graca
Actually you can react to incoming phone call just by adding listener to TelephonyManager
实际上,您只需将侦听器添加到 TelephonyManager 即可对来电做出反应
You define PhoneStateListener in your Service/Activity
您在 Service/Activity 中定义 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;
}
}
};
Then in onCreate method:
然后在 onCreate 方法中:
mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Finaly you clear the listener in onDestroy:
最后清除 onDestroy 中的侦听器:
mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
So much simpler in this case.
在这种情况下就简单多了。