如何在接听来电时暂停服务并在通话结束后恢复服务?

时间:2017-04-03 06:08:29

标签: android android-service phone-state-listener

我的应用程序服务简单,我需要在用户拨打或接听电话时暂停该服务,并且必须在通话结束后恢复服务。

以下是主要活动,如下所示: -

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(getApplicationContext(), MyService.class);
    startService(intent);


    TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    TelephonyMgr.listen(new TeleListener(),
            PhoneStateListener.LISTEN_CALL_STATE);
}


class TeleListener extends PhoneStateListener {
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
                        Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
                        Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Toast.makeText(getApplicationContext(), incomingNumber,
                        Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
                        Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }
}}

以下是我的服务类文件: -

public class MyService extends Service {
private static final String TAG = "MyService";

private boolean isRunning = false;

@Override
public void onCreate() {
    Log.i(TAG, "Service onCreate");

    isRunning = true;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.i(TAG, "Service onStartCommand");
    new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                }

                if (isRunning) {
                    Log.i(TAG, "Service running");
                }
            }

            stopSelf();
        }
    }).start();

    return Service.START_STICKY;
}


@Override
public IBinder onBind(Intent arg0) {
    Log.i(TAG, "Service onBind");
    return null;
}

@Override
public void onDestroy() {

    isRunning = false;

    Log.i(TAG, "Service onDestroy");
}}

MainActivity 中的 TeleListener类,我可以获取手机状态。我的问题是,当TelephonyManager等于 CALL_STATE_OFFHOOK 我需要暂停 MyService ,当TelephonyManager等于 CALL_STATE_IDLE 时,我需要恢复的为MyService

2 个答案:

答案 0 :(得分:0)

你需要使用onCallStateChanged方法。

将这些行放在你的onCreate()方法中,它将初始化TelephonyManager的对象并为你设置监听器。

TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
ListenToPhoneState listener = new ListenToPhoneState()
tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

内部类ListenToPhoneState的类定义将如下所示,

private class ListenToPhoneState extends PhoneStateListener {

    boolean callEnded=false;
    public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            UTILS.Log_e("State changed: " , state+"Idle");


            if(callEnded)
            {
                //you will be here at **STEP 4**
                //you should stop service again over here
            }
              else
              {
                //you will be here at **STEP 1**
             //stop your service over here,
                //i.e. stopService (new Intent(`your_context`,`CallService.class`));
                //NOTE: `your_context` with appropriate context and `CallService.class` with appropriate class name of your service which you want to stop.

              }


            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            UTILS.Log_e("State changed: " , state+"Offhook");
                //you will be here at **STEP 3**
             // you will be here when you cut call
            callEnded=true;
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            UTILS.Log_e("State changed: " , state+"Ringing");
                //you will be here at **STEP 2**

            break;


        default:
            break;
        }
    }

}

解释:在通话时,你的听众会经历以下状态,

第1步:TelephonyManager.CALL_STATE_IDLE

最初你的呼叫状态将是空闲的,这就是变量callEnded将具有值false的原因。

第2步:TelephonyManager.CALL_STATE_RINGING

现在您正在等待接收者接听您的电话

第3步:TelephonyManager.CALL_STATE_OFFHOOK

你切断了电话

第4步:TelephonyManager.CALL_STATE_IDLE

再次闲置

注意:如果您不想知道通话何时结束以及结束通话后应该做什么,那么只需删除callEnded变量并在您输入TelephonyManager.CALL_STATE_IDLE <块时停止服务/ p>

我希望它会有所帮助!!

答案 1 :(得分:0)

You cannot pause or resume a service, you can start your service again when call ends. Inside PhoneStateListener's TelephonyManager.CALL_STATE_OFFHOOK constant, start your service again! Using different states of PhoneStateListener as mentioned by @Tijo, you can decide when to start or stop your service.