拨打两次服务

时间:2015-01-06 16:19:35

标签: android call

  

我有这个代码,当有人叫我存储在数据库中但是当有人打电话给我这导致两次同时保存的号码和日期时,我的问题电话服务的所有代码..

CallBlocker = new BroadcastReceiver() {         

        @Override
        public void onReceive(Context context, Intent intent) 
        {
            telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class c = null;
            try {
                c = Class.forName(telephonyManager.getClass().getName());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Method m = null;
            try {
                m = c.getDeclaredMethod("getITelephony");
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            m.setAccessible(true);
            try {
                telephonyService = (ITelephony) m.invoke(telephonyManager);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            telephonyManager.listen(callBlockListener,PhoneStateListener.LISTEN_CALL_STATE);
        }

/// pording pnone state

     PhoneStateListener callBlockListener = new PhoneStateListener() 
        {
            public void onCallStateChanged(int state,String incomingNumber) 
            {
                if (state == TelephonyManager.CALL_STATE_RINGING)
                {

                    if (blockAll_cb.isChecked() && sarjda) 
                    {

                        Toast.makeText(getApplicationContext(), incomingNumber, Toast.LENGTH_SHORT).show();
                        telephonyService.endCall();
                        phoneNo = incomingNumber;


                        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy  HH:mm:ss");
                        String tarih = sdf.format(new Date());
                        gelenArama.createGelenArama(new GelenAramaItem(phoneNo, tarih));


                        Timer timer = new Timer();
                        TimerTask timerTask = new TimerTask() {
                            @Override
                            public void run() {
                                triggerNotification();
                            }
                        };
                        timer.schedule(timerTask, 20000);
                    }

                }
            }
        };          

    };
    IntentFilter filter = new IntentFilter("android.intent.action.PHONE_STATE");
    registerReceiver(CallBlocker, filter);

1 个答案:

答案 0 :(得分:0)

根据this,如果您的CallStateListener未声明为静态,则可能会发生这种情况。

来自TelephonyManager.CALL_STATE_RINGING calls twice while one call ringing

public class ServiceReceiver extends BroadcastReceiver {
    static CallStateListener phoneListener;

    @Override
    public void onReceive(final Context context, Intent intent) {

        if (phoneListener == null) {
            phoneListener = new CallStateListener();
            TelephonyManager tm = (TelephonyManager)context.getSystemService("phone");
            tm.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        }

    }

}