可活动收到无序广播(来电)意图系统的默认接收过吗?意图、系统

2023-09-13 00:02:38 作者:Absurd(荒谬)

下面是该方案:

这是活动显示(有效)。如果一个电话进来,活动应该得到的意图(发送来电屏幕的背景/从显示隐藏)和自己保持对用户可见。我不必然要燮preSS电话呼叫到来,因为我已经阅读了很多问题,这是不可能的公共API。

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.

我要的就是以某种方式使Android的默认来电屏幕隐藏我在上面活动。

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

在我的活动是可见这不等于有一个PHONE_STATE广播接收器,启动我的活动时,才需要此行为。后一个问题已经回答了这么多次。

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.

感谢您的时间。

推荐答案

这是我如何解决它:

的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 - 这显示了说你的屏幕被锁定的常规活动类的用户界面。它涵盖了屏幕即没有导航/状态栏的100%的面积。另外,HOME / MENU键已被禁用。这是我实现了:How我可以检测用户pressing HOME键在我的活动?

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。 :诀窍是不是主要的逻辑,但一个100毫秒的延迟。没有它,您的自定义(家)锁屏将你在手机上的呼叫系统默认来电屏幕每次被删除!

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!