我想检测拨出呼叫何时开始和结束,并希望在呼叫开始时启动服务,并在呼叫结束时结束服务。
这是我从另一个问题得到的代码,但不知道在哪里放置启动和停止活动以使其工作。
public class CallListener {
private CallStateListener callStateListener = null;
private OutgoingReceiver outgoingReceiver = null;
public CallListener(Context context) {
this.context = context;
callStateListener = new CallStateListener();
outgoingReceiver = new OutgoingReceiver();
}
private class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
doSomething1();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
doSomething2();
break;
case TelephonyManager.CALL_STATE_RINGING:
doSomething3();
break;
}
}
}
public class OutgoingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
doSomething4();
}
}
public void start() {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
context.registerReceiver(outgoingReceiver, intentFilter);
}
public void stop() {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
context.unregisterReceiver(outgoingReceiver);
}}
这是添加服务以使其工作:
public class MyService extends Service {
private CallListener call = null;
public MyService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
call = new CallListener(getApplicationContext());
call.start();
return(START_STICKY);
}
@Override
public void onDestroy() {
call.stop();
call.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
非常感谢您的帮助,我可能知道这很容易,但我只是不知道。
谢谢,祝你有个美好的一天.. !! :)
答案 0 :(得分:0)
我已经搜索过如何检测铃声/空闲/等等呼叫事件......没有成功:( 不幸的是,Android目前还没有解决方案......
在上一个问题/摘要帖子中对此进行了详细解释: