如何在另一个呼叫期间检测来电并在另一个呼叫期间停止服务?

时间:2017-07-11 06:47:44

标签: android service broadcastreceiver

当我接到电话时,我正在开始服务,但问题是,如果我接到另一个电话,我会在一个电话中,然后我需要停止服务。

      String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
            String caller = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);


Toast.makeText(context, "Ringing", Toast.LENGTH_SHORT).show();
            context.startService(new Intent(context, IdentityService.class)
                   .putExtra("number", caller)
                   .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            Log.d("RINGING","RINGING");




        }
        if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))
        {


            context.stopService(new Intent(context, IdentityService.class));
            context.stopService(new Intent(context, MessageService.class));




            Log.d("OFFHOOK","OFFHOOK");

        }

2 个答案:

答案 0 :(得分:0)

int flag=0; 
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
flag++;
if(flag>=2){
//stop service here
}
            String caller = 

intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

Toast.makeText(context, "Ringing", Toast.LENGTH_SHORT).show();
            context.startService(new Intent(context, IdentityService.class)
                   .putExtra("number", caller)
                   .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            Log.d("RINGING","RINGING");




        }
        if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))
        {


            context.stopService(new Intent(context, IdentityService.class));
            context.stopService(new Intent(context, MessageService.class));




            Log.d("OFFHOOK","OFFHOOK");

        }

答案 1 :(得分:0)

您不必停止服务。使用不同的负载启动相同的服务。按照以下链接中的服务处理程序示例

Example

public class HelloService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private boolean **callInprogreess**;


    private final class ServiceHandler extends Handler {
  public ServiceHandler(Looper looper) {
      super(looper);
  }
  @Override
  public void handleMessage(Message msg) {
         if(callInprogress){
            // DO your stuff how you want to handle the call 
          }else{
            // Do your stuff for call 1
            callInprogress=true;
          }

      }
  }
    @Override  
    public void onCreate() {
      HandlerThread thread = new HandlerThread("ServiceStartArguments",
        Process.THREAD_PRIORITY_BACKGROUND);
    thread.start();

    // Get the HandlerThread's Looper and use it for our Handler
    mServiceLooper = thread.getLooper();
    mServiceHandler = new ServiceHandler(mServiceLooper);
      }

      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

      // For each start request, send a message to start a job and deliver the
      // start ID so we know which request we're stopping when we finish the job
      Message msg = mServiceHandler.obtainMessage();
      msg.arg1 = startId;
      mServiceHandler.sendMessage(msg);

      // If we get killed, after returning from here, restart
      return START_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
      // We don't provide binding, so return null
      return null;
  }

  @Override
  public void onDestroy() {
    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
  }
}