保持当前的活动在前面,而来电来人在前面、来人

2023-09-06 22:39:01 作者:浪哩個浪

研究背景

我开发一个应用程序,这使得像lollpop调用。当有来电时它应该显示屏幕,并默认调用屏幕上的弹出应该是不可见的。

I am developing an app which makes calls like lollpop. When a call comes it should show a popup on the screen and the default calling screen should not be visible.

我做了什么

我建立广播接收器,目前我能够每一个电话来的时候显示一个弹出窗口。

I have build the broadcast receiver and currently I am able to show a popup every time a call come.

使用此code

public class PhoneCallReceive extends BroadcastReceiver {
    Class<?> c;
    public static ITelephony telephonyService = null;
    TelephonyManager telephony;
    Method m;
    public static int flag = 0;
    public static AudioManager audioManager;

    @Override
    public void onReceive(Context conk, Intent inter) {

        final Context context = conk;
        final Intent intent = inter;
        Thread pageTimer = new Thread() {
            public void run() {
                try {
                    sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        telephony = (TelephonyManager) context
                                .getSystemService(Context.TELEPHONY_SERVICE);
                        c = Class.forName(telephony.getClass().getName());
                        m = c.getDeclaredMethod("getITelephony");
                        m.setAccessible(true);
                        telephonyService = (ITelephony) m.invoke(telephony);
                        audioManager = (AudioManager) context
                                .getSystemService(Context.AUDIO_SERVICE);

                    } catch (Exception e) {

                        e.printStackTrace();
                    }
                    if (telephony.getCallState() == 1)

                        flag = 1;
                    Log.d("Rec", "reciever");
                    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

                    callingscreen("1", context, intent);
                }
            }
        };
        pageTimer.start();

    } 
    public void callingscreen(final String type, final Context context,
            final Intent intent) {
        Intent intentPhoneCall = new Intent(context.getApplicationContext(),
                CallerScreen.class);
        intentPhoneCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intentPhoneCall.putExtra(
                "number",
                intent.getExtras().getString(
                        TelephonyManager.EXTRA_INCOMING_NUMBER));
        intentPhoneCall.putExtra("type", type);
        context.startActivity(intentPhoneCall);
    }
} // class

问题

但问题是,这个弹出窗口显示在默认调用屏幕。我想要的显示用户当前屏幕上,他正致力于弹出。

But the Problem is that this popup is showing over the default calling screen. What I want to show the popup on the user current screen on which he was working.

问题

所以简单的问题是,我怎么能得到用户当前的屏幕,其弹出下面的所有功能?

So the simple question is that how can I get the user current screen with its all functionality below the popup?

请帮忙:)

推荐答案

该解决方案适用于每一个Android版本,包括棒棒糖 使用该广播reciever

This solution works on every version of android, including lollipop Use this broadcast reciever

class TeleListener extends PhoneStateListener {
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                // CALL_STATE_IDLE;
                Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
                        Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                // CALL_STATE_OFFHOOK;
                Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
                        Toast.LENGTH_LONG).show();
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                // CALL_STATE_RINGING, call intent here
                Intent i = null;
                i = new Intent(context,                  My_activity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
                break;
            default:
                break;
            }
        }
    }  

和注册像这样的的onCreate

and register it like this in onCreate

TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);  

是的,请不要错过。

And yes, please dont miss

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>  

干杯!你的问题就解决了​​;)

Cheers! Your problems is solved ;)