首次拨打电话时是否呼叫CALL_STATE_IDLE?

问题描述 投票:0回答:1

我正在创建一个运行服务的应用程序来捕获传出和传入的调用,并根据数字执行一些逻辑!

但是,当我第一次启动应用程序(安装后的示例)并退出应用程序并尝试呼叫时,电话状态监听器调用CALL_STATE_IDLE而不是CALL_STATE_OFFHOOK。但是从第二次调用本身开始按预期工作!当系统重新启动并在此之后第一次调用时也会发生这种情况!我使用BootCompleteReceiver广播接收器!

为什么会这样?请在这里建议修复!

我的服务代码:

public class CallService extends Service{
    PreferenceHelper finalprefs;
    Boolean updated = true;
    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= 26) {
            Notification notification=new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentText("").build();

            startForeground(1, notification);
        }
        Log.e("SERVICE","STARTED");
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.PHONE_STATE");
        finalprefs = new PreferenceHelper(getApplicationContext(),"1",0);
        registerReceiver(receiver, filter);
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(isMyServiceRunning(CallService.class)){

        }
        else{
            unregisterReceiver(receiver);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Log.e("> Nougout","> Nougout");
                //Util.scheduleJob(getApplicationContext());
                startForegroundService(new Intent(getApplicationContext(), CallService.class));
            }
            else {
                startService(new Intent(getApplicationContext(), CallService.class));
            }
        }
        Log.e("DESTROYED","sdsadasdasdasd");

    }


    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.e("onTaskRemoved","sdsadasdasdasd");

            if(isMyServiceRunning(CallService.class)){

            }
            //startService(new Intent(getApplicationContext(), CallService.class));
            else{
                unregisterReceiver(receiver);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    Log.e("> Nougout","> Nougout");
                    //Util.scheduleJob(getApplicationContext());
                    startForegroundService(new Intent(getApplicationContext(), CallService.class));
                }
                else {
                    startService(new Intent(getApplicationContext(), CallService.class));
                }
            }
        super.onTaskRemoved(rootIntent);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        String number ="";
        TelephonyManager telManager;
        Context context;
        boolean startedCall = false;
        boolean endedCall = false;
        int ring = 0;
        @Override
        public void onReceive(Context context, Intent intent)
        {
            this.context=context;
            boolean startedCall = false; // New added boolean
            telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
        }


        private final PhoneStateListener phoneListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                try {
                    switch (state) {
                        case TelephonyManager.CALL_STATE_RINGING: {
                            //INCOMING CALL
                            if(incomingNumber!=null && ring <= 0) {
                                bussinessLogic(incomingNumber);
                                ring++;
                                Log.e("CALL_STATE_RINGING","CALL_STATE_RINGING");
                            }
                            break;
                        }
                        case TelephonyManager.CALL_STATE_OFFHOOK: {
                            //Outgoing call
                            Log.e("OUTGOING CALL","CALL NUMBER - "+incomingNumber);
                            startedCall  = true; // Newly added code
                            if(incomingNumber!=null) {
                                if(incomingNumber.equals("#8678")){
                                    disconnectPhoneItelephony(context);
                                    if(finalprefs.getBoolean("hidden",false)){
                                        Intent i = new Intent(getApplicationContext(),PinverificationHidden.class);
                                        startActivity(i);
                                    }
                                    else{
                                        Intent i = new Intent(getApplicationContext(),Pinverification.class);
                                        startActivity(i);
                                    }

                                }
                                else{
                                    bussinessLogic(incomingNumber);
                                }

                                Log.e("CALL_STATE_OFFHOOK","CALL_STATE_OFFHOOK");
                            }
                            break;
                        }
                        case TelephonyManager.CALL_STATE_IDLE: {
                            //CALL DISCONNECTED
                            ring = 0;
                            Log.e("CALL_STATE_IDLE --","CALL_STATE_IDLE --");
                            if(startedCall) {
                                Log.e("CALL_STATE_IDLE","CALL_STATE_IDLE");
                            }
                            break;
                        }
                        default: {
                            break;
                        }

                    }
                } catch (Exception ex) {

                }
                //telManager.listen(phoneListener, PhoneStateListener.LISTEN_NONE);
            }
        };
}

我在活动开始时开始这项服务!此外,我在PHONE STATE的广播接收器中开始此活动!

我的BoradcastReciever是:

public class CallBarring extends BroadcastReceiver
{
    PreferenceHelper finalprefs;
    // This String will hold the incoming phone number
    private String number;

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        finalprefs = new PreferenceHelper(context,"1",0);
        // If, the received action is not a type of "Phone_State", ignore it
         if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) 
             return;
         else 
         {
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                 Log.e("> RECIEVER","> RECIEVER");
                 if(!isMyServiceRunning(context)){
                     context.startForegroundService(new Intent(context, CallService.class));
                 }
             }
             else{
                 if(!isMyServiceRunning(context)){
                     context.startService(new Intent(context, CallService.class));
                 }
             }
         }
     }
}
android android-broadcast phone-state-listener
1个回答
0
投票

当你启动Service时,它会注册BroadcastReceiver,但不会注册PhoneStateListener。您只在PhoneStateListener被触发后注册了BroadcastReceiver(由于电话状态改变)。此时,您注册了PhoneStateListener,但您将错过第一次手机状态更改。这就是它第一次不起作用的原因。

© www.soinside.com 2019 - 2024. All rights reserved.