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

后端 未结 2 1432
伪装坚强ぢ
伪装坚强ぢ 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

    This is how I solved it :

    Manifest.xml

    
    ...
    
    
        
        
    
    
    
        
            
             
            
     
    

    MyPhoneBroadcastReceiver.java

    public void onReceive(final Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        ...
        if (extras != null) {
        String state = extras.getString(TelephonyManager.EXTRA_STATE);
    
         if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                 new Handler().postDelayed(new Runnable() {
              public void run() {
                  Intent intentPhoneCall = new Intent("android.intent.action.ANSWER");
                          intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startActivity(intentPhoneCall);
                      }
                 }, 100);
             }
        }
    }
    

    LockScreenActivity.java - A regular activity class with UI that shows up saying your screen is locked. It covers 100% area of your screen i.e. no navigation/status bar. Also the HOME/MENU keys have been disabled. This is how I achieved that : How can I detect user pressing HOME key in my activity?

    P.S. : The trick is not the main logic but a 100ms delay. Without it your custom(home) lock screen will be removed by the system default incoming call screen everytime you get a call on the phone!

提交回复
热议问题