检测传出呼叫已启动并结束

我想要检测传出呼叫何时启动并结束,并且希望在呼叫已启动时启动服务并在呼叫结束时结束服务。检测传出呼叫已启动并结束

下面是我从另一个问题得到的代码,但不知道将启动和停止活动放在哪里以使其工作。

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;

}

}

非常感谢您的帮助,我可以知道这是容易的,但我只是不知道。

谢谢你,祝你有个美好的一天。 :)

回答:

我已经搜查了如何检测振铃一样/空闲的/ etc ...没有成功:( 不幸的是,此时在Android中没有解决......

这是很好呼叫事件在此前的问题/总结帖子中解释:

Detecting outgoing call answered on Android

以上是 检测传出呼叫已启动并结束 的全部内容, 来源链接: utcz.com/qa/263194.html

回到顶部