Can an activity receive an unordered broadcast(incoming call) intent before system's default receiver?

后端 未结 2 1433
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 16:59

Here is the scenario :

An activity is displayed(active). If a phone call comes, the activity should receive the intent (send the \"incoming call screen\" to the back

2条回答
  •  醉话见心
    2020-12-05 17:04

    Ya sanjana is right that code works for me.. but instead of giving postdelay 100msec give more then 1.5 sec.. Bcoz to start incoming screen needs 800 to 1000msec.. I copied her code only but modified a bit.. Try this works fine...

    @Override
    public void onReceive(final Context context, Intent intent) {
    
        Bundle extras = intent.getExtras();
    
        if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            final String incomingNumber = extras.getString("incoming_number");
    
            Handler callActionHandler = new Handler();
    
            Runnable runRingingActivity = new Runnable() {
    
                @Override
                public void run() {
                    Intent intentPhoneCall = new Intent("android.intent.action.ANSWER");
                    intentPhoneCall.putExtra("INCOMING_NUM", incomingNumber);
                    intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intentPhoneCall);
                }
            };
    
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                callActionHandler.postDelayed(runRingingActivity, 2000);
    
            }
    
            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                callActionHandler.removeCallbacks(runRingingActivity);
                // setResultCode(Activity.RESULT_CANCELED);
            }
    
        }
    }
    

    Then use PhoneStateListener at receiver class side.. I used like this....

    /*********** My receiver class onCreate() contains ************/
    TelephonyManager telephony = (TelephonyManager)    
    getSystemService(getApplicationContext().TELEPHONY_SERVICE);
            telephony.listen(myListener, PhoneStateListener.LISTEN_CALL_STATE);
    
    class MyPhoneStateListener extends PhoneStateListener {
    
         Activity curActivity;
    
         public MyPhoneStateListener(Activity _curActivity){
             this.curActivity = _curActivity;
    
         }
    
            public void onCallStateChanged(int state, String incomingNumber) {
                switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.i("TEST APP", "Cal End");                   
                    curActivity.finish();                   
                    break;
    
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.i("TEST APP", "Hello");                 
                    break;
    
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.i("TEST APP", "Calling...");
    
                    break;
                }
    
            }
        }
    

提交回复
热议问题