Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

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

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 background/hide it from the display) and itself remain visible to the user. I dont necessarily want to suppress the incoming phone call as I have read in a lot of questions that it is not possible with public APIs.

All I want is to somehow make that android's default incoming call screen hidden by my activity on the top.

This behavior is only required when my activity is visible which is NOT EQUAL TO having a PHONE_STATE broadcast receiver to start my activity. The latter question has been answered a number of times on SO.

Please help me. I have been looking for directions for almost a day now.

Thanks for your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is how I solved it :

Manifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
...
<receiver android:name=".MyPhoneBroadcastReceiver">
<intent-filter android:priority="99999">
    <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

<activity android:name=".LockScreenActivity" android:noHistory="true" android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.ANSWER" />
         <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>    
 </activity>

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...