在Android本机来电屏幕上弹出窗口,例如真正的来电者Android应用

我正在为Android中的来电开发广播接收器,并且在收到来电时,我想在本机来电屏幕上弹出一个弹出窗口。


我完成了该代码。但是现在的问题是,在Android 4.1(Jelly Bean)API级别17中,当电话响起时,会以PHONE_STATE形式出现OFF HOOK,如果我正在调用活动,则会调用该活动,但不会执行其下的代码。我正在列出代码:


我的广播接收器

package com.example.popwindowonincomingcallscreen;


import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;


import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.telephony.TelephonyManager;

import android.util.Log;


public class IncomingBroadcastReceiver extends BroadcastReceiver {


    @Override

    public void onReceive(Context context, Intent intent) {


        Log.d("IncomingBroadcastReceiver: onReceive: ", "flag1");


        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        Log.d("IncomingBroadcastReceiver: onReceive: ", state);

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)

                || state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {


            Log.d("Ringing", "Phone is ringing");


            Intent i = new Intent(context, IncomingCallActivity.class);

            i.putExtras(intent);

            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

            Wait.oneSec();

            context.startActivity(i);

        }

    }

}

try {

    Log.d("IncomingCallActivity: onCreate: ", "flag2");

}

该代码未在Android 4.1(Jelly Bean)中执行,但在其他版本中仍可运行。


我已经尝试了几乎所有方法。该代码在本机呼叫屏幕上显示了半透明的活动,并且不会阻止后台控件,例如接听电话。但我希望它像真正的呼叫者一样。我已附上快照,以了解真正的呼叫者如何在来电屏幕上显示窗口。


如何为Android应用程序实现此功能?


真正的呼叫者是这样工作的:


http://img2.mukewang.com/5da562280001ca6c01900336.jpg

我现在的输出:


http://img2.mukewang.com/5da5622b0001123d05530507.jpg

更新1

赏金后,我也没有得到想要的确切东西,但是我会回到一切。我正在努力。无论如何,此代码适用于大多数Android手机。如果有人要使用并赶上解决方案,请在此处写下,以便每个人都能从中受益。


更新2

我尝试在广播接收器的onReceive方法中实现Toast,因为toast是Android的本机组件,但也未在Android 4.1(Jelly Bean)中显示。


我的想法是在广播接收器的onReceive方法中实现Toast,然后根据我们的需求更改其设计并调整其显示时间。但是还有一个问题是,findViewById在广播接收器中不起作用,所以我认为我们必须以编程方式制作LinearLayout来自定义吐司。


哔哔one
浏览 1292回答 3
3回答

HUWWW

我不确定您的自定义GUI是否将始终位于默认GUI的顶部,因为系统广播接收器和您的接收器都试图将其GUI显示在屏幕顶部。我们不确定哪个首先被调用,但是要使您的GUI出现在屏幕顶部的一项棘手的工作是在电话响起1-2秒后,使用您的活动处理程序来调用您的活动。new Handler().postDelayed(new Runnable() {     @Override     public void run() {         // TODO Auto-generated method stub         Intent intent = new Intent(context, AcceptReject.class);         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         context.startActivity(intent);     } }, 2000);希望对您有帮助。

繁花如伊

我刚刚在Android 4.2(Jelly Bean)模拟器上进行了测试,它可以像truecaller一样阻塞整个来电屏幕,从而完美地发挥了作用:public void onReceive(Context context, Intent intent) {&nbsp; &nbsp; WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);&nbsp; &nbsp; WindowManager.LayoutParams params = new WindowManager.LayoutParams(&nbsp; &nbsp; &nbsp; &nbsp; LayoutParams.MATCH_PARENT,&nbsp; &nbsp; &nbsp; &nbsp; LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |&nbsp; &nbsp; &nbsp; &nbsp; WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,&nbsp; &nbsp; &nbsp; &nbsp; WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |&nbsp; &nbsp; &nbsp; &nbsp; WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,&nbsp; &nbsp; &nbsp; &nbsp; PixelFormat.TRANSPARENT);&nbsp; &nbsp; params.height = LayoutParams.MATCH_PARENT;&nbsp; &nbsp; params.width = LayoutParams.MATCH_PARENT;&nbsp; &nbsp; params.format = PixelFormat.TRANSLUCENT;&nbsp; &nbsp; params.gravity = Gravity.TOP;&nbsp; &nbsp; LinearLayout ly = new LinearLayout(context);&nbsp; &nbsp; ly.setBackgroundColor(Color.RED);&nbsp; &nbsp; ly.setOrientation(LinearLayout.VERTICAL);&nbsp; &nbsp; wm.addView(ly, params);}在清单中:<receiver android:name=""&nbsp; android:enabled="true" >&nbsp; &nbsp; <intent-filter android:priority="-1">&nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.PHONE_STATE" />&nbsp; &nbsp; </intent-filter></receiver>
打开App,查看更多内容
随时随地看视频慕课网APP