2014-07-03 23 views
0

我創建了一個調用數字的應用程序。它正在工作,但我想在30秒後關閉通話。我怎樣才能做到這一點? 我的代碼是:如何停止使用android中的代碼調用?

 MyTimer = new CountDownTimer(60000, 30000) { 

      public void onTick(long millisUntilFinished) { 


       intent.setData(Uri.parse("tel:" + num2 + "")); 
       startActivity(intent); 


          } 

      public void onFinish() { 


      } 
     }.start(); 

回答

0

您需要一些反思。首先,創建這個接口:

package com.android.internal.telephony; 
public interface ITelephony { 
    boolean endCall(); 
    void answerRingingCall(); 
    void silenceRinger(); 
} 

而結束通話本:從here

private void endCall() { 
    TelephonyManager telephonyManager = (TelephonyManager) this 
      .getSystemService(Context.TELEPHONY_SERVICE); 
    Class<?> myClass = null; 
    try { 
     myClass = Class.forName(telephonyManager.getClass().getName()); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
    Method method = null; 
    try { 
     method = myClass.getDeclaredMethod("getITelephony"); 
    } catch (NoSuchMethodException e) { 
     e.printStackTrace(); 
    } 
    method.setAccessible(true); 
    ITelephony telephonyService = null; 
    try { 
     telephonyService = (ITelephony) method.invoke(telephonyManager); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     e.printStackTrace(); 
    } 
    telephonyService.endCall(); 
} 

修改答案。

編輯:添加例子:這個添加一些按鈕,點擊

//Start call 
    makeCall(phoneNumber); 
    //wait several seconds 
    try { 
     long time = holdCallSeconds * 1000; 
     Thread.sleep(time); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
    //hang up 
    endCall(); 


void makeCall(String phoneNumber) { 
    Intent intent = new Intent(Intent.ACTION_CALL); 
    intent.setData(Uri.parse(phoneNumber)); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    intent.addFlags(Intent.FLAG_FROM_BACKGROUND); 
    startActivity(intent); 
} 
相關問題