在 Android 本机来电屏幕上弹出窗口,如真正的来电 Android 应用程序 [英] Pop up window over Android native incoming call screen like true caller Android app

查看:32
本文介绍了在 Android 本机来电屏幕上弹出窗口,如真正的来电 Android 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a broadcast receiver for incoming calls in Android and on getting incoming calls I want to inflate a pop up over the native incoming call screen.

我完成了那个代码.但现在的问题是在 Android 4.1 (Jelly Bean) API level 17 当电话响起时,PHONE_STATEOFF HOOK 的形式出现,如果我正在调用一个活动,它会被调用,但它下面的代码没有't 被执行.我列出了代码:

I completed that code. But now the problem is that in the Android 4.1 (Jelly Bean) API level 17 when a phone rings, the PHONE_STATE is coming as OFF HOOK, and if I am calling an activity, it gets called, but the code under it doesn't get executed. I am listing the code:

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);
        }
    }
}

我正在调用的活动:

import android.app.Activity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View.MeasureSpec;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class IncomingCallActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

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

            */ After this line, the code is not executed in Android 4.1 (Jelly Bean) only/*

            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);

            getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

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

            setContentView(R.layout.main);

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

            String number = getIntent().getStringExtra(
                    TelephonyManager.EXTRA_INCOMING_NUMBER);
            TextView text = (TextView) findViewById(R.id.text);
            text.setText("Incoming call from " + number);
        } 
        catch (Exception e) {
            Log.d("Exception", e.toString());
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

之后

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

该代码无法在 Android 4.1 (Jelly Bean) 中执行,但在其他版本中可以运行.

The code is not executing in Android 4.1 (Jelly Bean), but in other versions it is working.

我尝试了几乎所有我能做的方法.此代码在本机呼叫屏幕上显示半透明活动,并且不会阻止后台控制,例如拿起电话.但我希望它像真正的来电者一样.我附上了一张关于真实来电者如何在来电屏幕上显示窗口的快照.

I have tried almost all ways I can do. This code is displaying an translucent activity over the native call screen, and it doesn't block background controls, like picking up the phone. But I want it like true caller. I have attached an snapshot on how the true caller is displaying a window on the incoming call screen.

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

How can I achieve this functionality for an Android app?

真正的来电者是这样工作的:

This is how a true caller works:

我目前的输出:

在赏金之后,我也没有得到我正在寻找的确切东西,但我会全部回来;我正在研究它.无论如何,此代码适用于大多数 Android 手机.如果有人要使用并找到解决方案,请写在这里,以便每个人都能从中受益.

After bounty also I am not getting the exact thing I am looking for, but I will get back to all; I am working upon it. Anyway, this code works for most Android phones. If anybody is going to use and catch the solution for it, please write here so that everybody can get the benefit.

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

I tried to implement Toast in the broadcast receiver's onReceive method because toast is a native component of Android, but it is also not getting displayed in Android 4.1 (Jelly Bean).

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

My idea was to implement Toast in the broadcast receiver's onReceive method and afterwards changing its design according to our needs and tuning its duration of display. But one more problem is that findViewById doesn't work in the broadcast receiver, so I think we have to make a LinearLayout programmatically for customizing the toast.

推荐答案

我不确定您的自定义 GUI 是否始终位于默认 GUI 之上,因为系统广播接收器和您的接收器都在尝试显示其 GUI在屏幕顶部.我们不确定首先调用哪一个,但让您的 GUI 位于屏幕顶部的一项棘手工作是当电话响铃时,在 1-2 秒后使用处理程序调用您的活动.

I am not sure that your custom GUI will always be on top of the default one, because the system broadcast receiver and your receiver are both trying to display its GUI on top of the screen. We are not sure which one is called first, but one tricky work to make your GUI on top of the screen is when the phone is ringing call your activity after 1-2 second(s) used handler for that.

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 本机来电屏幕上弹出窗口,如真正的来电 Android 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆