在来电时将当前活动保持在前面

时间:2015-04-03 08:11:15

标签: android

化背景

我正在开发一个像lollpop一样调用的应用程序。当有来电时,它应该在屏幕上显示一个弹出窗口,并且默认的呼叫屏幕不应该是可见的。

我做了什么

我已经构建了广播接收器,目前我每次来电时都能显示一个弹出窗口。

使用此代码

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

问题

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

问题

所以简单的问题是,如何在弹出窗口下面显示其所有功能的用户当前屏幕?

请帮助:)

1 个答案:

答案 0 :(得分:-1)

此解决方案适用于每个版本的android,包括棒棒糖
使用此广播接收器

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中将其注册为

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

是的,请不要错过

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

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