2017-04-03 167 views
0

我的應用程序有一個簡單的服務,當用戶撥打或接聽電話時,我需要暫停該服務,服務必須在通話結束後恢復。如何在收到來電時暫停服務,並在通話結束後恢復服務?

這裏是我的主要活動樣子: -

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

TeleListener類MainActivity我能夠獲取電話狀態。我的問題是,當TelephonyManager等於CALL_STATE_OFFHOOK我需要暫停爲MyService當TelephonyManager等於CALL_STATE_IDLE我需要恢復爲MyService

回答

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

塊之後完成,我希望它會有幫助!

+0

我已經宣佈所有這些。我需要暫停並恢復CALL_STATE_OFFHOOK和CALL_STATE_IDLE – Karthik

+0

上的服務。它的工作正常,但它只在應用程序打開時工作,但我需要像服務一樣隨時工作。 – Karthik

0

您無法暫停或恢復服務,您可以在通話結束時再次啓動服務。裏面PhoneStateListenerTelephonyManager.CALL_STATE_OFFHOOK不斷,再次啓動您的服務!使用@Tijo提到的PhoneStateListener的不同狀態,您可以決定何時開始或停止服務。