【问题标题】:Call two times service呼叫两次服务
【发布时间】:2015-01-06 16:19:35
【问题描述】:

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

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

///根据pnone状态

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

【问题讨论】:

  • 你有什么问题?我们不是通灵者 - 仅显示您的代码和不清楚的标题不足以了解您需要什么帮助。
  • 当有人打电话给我时,我在sql数据库中保存了电话号码,但是这段代码保存了两次电话。

标签: android call


【解决方案1】:

根据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);
        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    相关资源
    最近更新 更多