运行呼叫列表并结束它们

时间:2015-07-27 22:11:34

标签: java android arraylist telephonymanager

我有号码列表,我必须打电话给foreach号码并在10秒后结束并开始新的通话。 代码正在努力拨打电话但是在完成所有列表项目之后,呼叫是不是停止继续使用第一个号码进行呼叫?

private void StartCalling()
    {
        try
        {
            if(allNumbers.size() != 0)
            {
                countDown = allNumbers.size();
                for (Iterator<String> iter = allNumbers.listIterator(); iter.hasNext(); ) {
                        final String num = iter.next();
                        final TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                        PhoneStateListener phoneStateListener = new PhoneStateListener() {
                            @Override
                            public void onCallStateChanged(int state, String incomingNumber) {
                                if (state == TelephonyManager.CALL_STATE_IDLE) {
                                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                                    callIntent.setData(Uri.parse("tel:" + num));
                                    startActivity(callIntent);
                                }
                            }
                        };
                        tManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
                }
            }else
            {
                Message.message(CTX,"Not a calling number..");
            }
        }catch(Exception ex)
        {
            Message.message(CTX,"" + ex);
        }

    }

并在10秒后结束通话。

public class PhoneCall extends BroadcastReceiver {
    CountDownTimer mCountDownTimer;
    long countdownPeriod;
    long remainingCount;
    @Override
    public void onReceive(final Context context, Intent intent) {
        try {
            TelephonyManager tManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            PhoneStateListener phoneStateListener = new PhoneStateListener() {
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                    if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                            countdownPeriod = 10000;
                            createCountDownTimer(context);
                    }
                }
            };
            tManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createCountDownTimer(final Context context) {
        mCountDownTimer = new CountDownTimer(countdownPeriod, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                long l = millisUntilFinished/1000;
                countdownPeriod = l;
            }

            @Override
            public void onFinish() {
                TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                Class c = null;
                try {
                    c = Class.forName(tm.getClass().getName());
                    if(c != null)
                    {
                        Method m = tm.getClass().getDeclaredMethod("getITelephony");
                        m.setAccessible(true);
                        Object iTelephony = m.invoke(tm);
                        Method m2 = iTelephony.getClass().getDeclaredMethod("endCall");
                        m2.invoke(iTelephony);
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

0 个答案:

没有答案