Android 来电屏幕上的活动弹出窗口

Android 来电屏幕上的活动弹出窗口,android,Android,我正在尝试覆盖来电屏幕-我知道我无法更改它,所以我正在尝试在顶部弹出一个活动 我的代码工作正常,除非手机闲置了几分钟 我的代码: AndroidManifest.xml: <?xml version="1.0" encoding="utf-8" ?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myfirstapp" a

我正在尝试覆盖来电屏幕-我知道我无法更改它,所以我正在尝试在顶部弹出一个活动

我的代码工作正常,除非手机闲置了几分钟

我的代码:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.myfirstapp"
      android:versionCode="7"
      android:versionName="7">
    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="10"></uses-sdk>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <receiver android:name=".MyPhoneBroadcastReceiver" android:enabled="true">
            <intent-filter android:priority="99999">
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
        <activity
            android:name=".Call" >
        </activity>
    </application>
</manifest>
public class MyPhoneBroadcastReceiver extends BroadcastReceiver{

    public void onReceive(final Context context, Intent intent) {

        Thread pageTimer = new Thread(){
            public void run(){
                try{
                    sleep(700);
                } catch (InterruptedException e){
                    e.printStackTrace();
                } finally {
                    Intent i = new Intent();
                    i.setClass(context, Call.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  
                    i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
                    i.putExtra("INCOMING_NUMBER", incomingNumber);
                    i.setAction(Intent.ACTION_MAIN);
                    i.addCategory(Intent.CATEGORY_LAUNCHER);
                    context.startActivity(i);
                }
            }
        };
        pageTimer.start();
    }
}
package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;

public class Call extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow(). addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        getWindow().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        setContentView(R.layout.main);
    }
}
Call.java:

<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.myfirstapp"
      android:versionCode="7"
      android:versionName="7">
    <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="10"></uses-sdk>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <receiver android:name=".MyPhoneBroadcastReceiver" android:enabled="true">
            <intent-filter android:priority="99999">
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
        <activity
            android:name=".Call" >
        </activity>
    </application>
</manifest>
public class MyPhoneBroadcastReceiver extends BroadcastReceiver{

    public void onReceive(final Context context, Intent intent) {

        Thread pageTimer = new Thread(){
            public void run(){
                try{
                    sleep(700);
                } catch (InterruptedException e){
                    e.printStackTrace();
                } finally {
                    Intent i = new Intent();
                    i.setClass(context, Call.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  
                    i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
                    i.putExtra("INCOMING_NUMBER", incomingNumber);
                    i.setAction(Intent.ACTION_MAIN);
                    i.addCategory(Intent.CATEGORY_LAUNCHER);
                    context.startActivity(i);
                }
            }
        };
        pageTimer.start();
    }
}
package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;

public class Call extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow(). addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        getWindow().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        setContentView(R.layout.main);
    }
}
顺便说一句,我试着在睡觉前等待屏幕唤醒(700),但没有效果 (在MyPhoneBroadcastReceiver.java中)


当手机屏幕关闭且有来电时,有更多的工作要做(唤醒、处理键盘保护视图…),因此通话中的活动需要更长的时间才能显示,这导致您的通话活动开始时间早于通话中的活动开始时间-->通话中的活动在顶部
通话中活动不需要显示确切的时间(您已经尝试并看到700毫秒是不够的)

我的解决方案:继续跟踪呼叫活动的状态:

  • 如果它仍然在顶部,并且用户没有取消它(或任何要删除的条件) 退出),只需使用处理程序继续跟踪即可
  • 如果有任何其他活动,请转到前台,然后调用活动 试着回到顶端
我的示例活动:

public class MainActivity extends Activity {
    private ActivityManager mActivityManager;
    private boolean mDismissed = false;

    private static final int MSG_ID_CHECK_TOP_ACTIVITY = 1;
    private static final long DELAY_INTERVAL = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

        mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        mHandler.sendEmptyMessageDelayed(MSG_ID_CHECK_TOP_ACTIVITY,
                DELAY_INTERVAL);
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == MSG_ID_CHECK_TOP_ACTIVITY && !mDismissed) {
                List<RunningTaskInfo> tasks = mActivityManager
                        .getRunningTasks(1);
                String topActivityName = tasks.get(0).topActivity
                        .getClassName();
                if (!topActivityName.equals(MainActivity.this
                        .getComponentName().getClassName())) {
                    // Try to show on top until user dismiss this activity
                    Intent i = new Intent();
                    i.setClass(MainActivity.this, MainActivity.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
                    startActivity(i);
                }
                sendEmptyMessageDelayed(MSG_ID_CHECK_TOP_ACTIVITY,
                        DELAY_INTERVAL);
            }
        };
    };

}
公共类MainActivity扩展活动{
私人活动经理mActivityManager;
私有布尔mDismissed=false;
私有静态最终int MSG\u ID\u CHECK\u TOP\u ACTIVITY=1;
专用静态最终长延迟_间隔=100;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Window=getWindow();
addFlags(WindowManager.LayoutParams.FLAG\u discover\u KEYGUARD);
mActivityManager=(ActivityManager)getSystemService(Context.ACTIVITY_服务);
mHandler.sendEmptyMessageDelayed(消息\u ID\u CHECK\u TOP\u活动,
延迟时间间隔);
}
私有处理程序mHandler=新处理程序(){
public void handleMessage(android.os.Message msg){
如果(msg.what==msg\u ID\u CHECK\u TOP\u ACTIVITY&&!mDismissed){
列表任务=mActivityManager
.getRunningTasks(1);
字符串topActivityName=tasks.get(0).topActivity
.getClassName();
如果(!topActivityName.equals)(MainActivity.this
.getComponentName().getClassName()){
//尝试在顶部显示,直到用户取消此活动
意图i=新意图();
i、 setClass(MainActivity.this,MainActivity.class);
i、 添加标志(意图、标志、活动、清除、顶部);
i、 addFlags(WindowManager.LayoutParams.FLAG在锁定时显示);
星触觉(i);
}
sendEmptyMessageDelayed(消息\u ID\u CHECK\u TOP\u活动,
延迟时间间隔);
}
};
};
}

我使用您的代码,它几乎可以正常工作。在此活动中,我添加了应答按钮,但当我(成功)接受呼叫时,此活动不会消失。如何管理?感谢您可以使用此api:-->mHandler.removeMessage(MSG\u ID\u CHECK\u TOP\u活动);这是行不通的。它打开对话框“活动”一次,MDISMSessed的值变为true,然后再次不调用处理程序,然后该活动转到调用屏幕后面。@R.Y这取决于将MDISMSessed设置为true的位置。在我上面的回答中,只有当用户关闭对话框时,它才会变为真。