0

嘿,我有一个 OutgoingCallReceiver,它可以检测拨出电话何时发生。如果拨打了一个特殊号码,它就会启动一项服务。

但是现在,我遇到了一个问题,即当拨出电话结束时我需要停止相同的服务。

有什么好的方法吗?谢谢!!

ublic class OutgoingCallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub


    boolean ringing = false, offhook = false;

    Intent i = new Intent(arg0, MyServices.class);
    Intent ii = new Intent(arg0, RecorderService.class);
    Bundle bundle = arg1.getExtras();

    if(null == bundle)
            return;


    String phonenumber = arg1.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

    Log.i("OutgoingCallReceiver",phonenumber);
    Log.i("OutgoingCallReceiver",bundle.toString());


    if(phonenumber.equals("#1234") || bundle.toString().equals("#1234")) { 

        ii.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        arg0.startService(ii);
    }


    String action = arg1.getAction();
    if (Intent.ACTION_NEW_OUTGOING_CALL.equals(action)) {

    } else{
        arg0.stopService(ii);
    }



    } 
}    

有代码。

感谢所有的帮助,任何帮助表示赞赏。谢谢!

4

1 回答 1

0

我的服务类:

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;
}
}

类调用监听器:

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);
}
}
于 2013-07-17T14:02:33.350 回答