简体   繁体   English

Android:如何在for循环内调用意图?

[英]Android: How to call an intent inside for loop?

In my application I need to make a call by an intent several times. 在我的应用程序中,我需要多次意图调用。 This is the scenario. 这是方案。 1. User enters a phone number, delay time and number of attempts in the interface. 1.用户在界面中输入电话号码,延迟时间和尝试次数。 2. Once user press the dial button in my UI it will make a call and will hang up after the specified delay. 2.一旦用户在我的UI中按下拨号按钮,它将拨打电话并在指定的延迟后挂断电话。 3. Once hung up, the app needs to make a call again until the entered number of attempts is satisfied. 3.挂断后,应用程序需要再次拨打电话,直到满足输入的尝试次数。

I have used android ITelephony.aidl and java reflection to call hangup function. 我已经使用了android ITelephony.aidl和java反射来调用hangup函数。 For one attempt I can successfully terminate the call. 我可以尝试一次成功终止通话。 The problem occurs when I need to make several attempts. 当我需要进行几次尝试时,就会出现问题。 This is my code: 这是我的代码:

private void sendCommand(String number, int delay, int tries){
    for(int i=0;i<tries;i++){
        callPhone(number);
        delayTimer(delay);
    }
}
private void callPhone(String number){
    String num = "0776355524";
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    if(!(number.equalsIgnoreCase(""))){
        num = number;
    }   
    callIntent.setData(Uri.parse("tel:"+num));
    startActivity(callIntent);
}
private void delayTimer(int delay){
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // Do something after delay
            if(isOffhook()){
                cancelCall();
            }

        }
    }, delay);
}

private boolean isOffhook(){
     TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
    Log.v(TAG, "Get getTeleService...");
    Class c = Class.forName(tm.getClass().getName());
    Method m = c.getDeclaredMethod("getITelephony");
    m.setAccessible(true);
    telephonyService = (ITelephony) m.invoke(tm);

    return telephonyService.isOffhook();

    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "Error in accessing Telephony Manager: "+ e.toString());
         telephonyService = null;
    }

    return false;
}

private void cancelCall(){
    Log.d(TAG, "Ending call..."+ incomingNumber);
    try {
        if(telephonyService != null)
        telephonyService.endCall();
    } catch (Exception e) {
        e.printStackTrace();
        Log.d(TAG, "Error in accessing Telephony Manager: "+ e.toString());
    }
}

What am I doing wrong in my code. 我在代码中做错了什么。 Thanks in advance, Hasala 预先感谢,哈萨拉

This can't work: 这行不通:

for(int i=0;i<tries;i++){
    callPhone(number);
    delayTimer(delay);
}

This loop calls the number, then posts a Runnable to hangup after some period of time. 此循环调用该号码,然后过一段时间后发布一个Runnable使其挂断。 Then immediately calls the number again. 然后立即再次拨打该号码。 You need to wait for the delay before calling the number again. 您需要等待一段时间才能再次拨打该号码。 Do this once (not in a loop): 这样做一次(不是循环):

    callPhone(number);
    delayTimer(delay);

Then, in your Runnable , do this: 然后,在您的Runnable ,执行以下操作:

        // Do something after delay
        if(isOffhook()){
            cancelCall();
        }
        // now call the number again
        callPhone(number);
        // reschedule the delay
        delayTimer(delay);

You'll have to keep track of the number of tries somewhere, so you know when to stop. 您必须跟踪某处的尝试次数,以便知道何时停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM