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

クラス MyService:

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

クラス CallListener:

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 に答える