Android实现监听来电,并显示提示框

来源:互联网 发布:数控加工及编程 编辑:程序博客网 时间:2024/05/21 10:43

最近老板来个要求:安装了程序之后,客户打电话来能在电话界面显示客户信息。郁闷,自己客户自己不知道维护呀,真的懒。

1.开机启动
2.监听系统电话状态改变
3.显示悬浮窗

一、首先创建一个广播

public class BootBroadcastReceiver extends BroadcastReceiver {       private WindowManager wm;    private View view;    @Override    public void onReceive(Context context, Intent intent) {        TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);        tm.listen(new PhoneListener(context), PhoneStateListener.LISTEN_CALL_STATE);    }    private void showWindow(String phone, String companyName, String infor, Context context) {        wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);        WindowManager.LayoutParams params = new WindowManager.LayoutParams();        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;//设置窗口类型在所有窗口之上//        params.type = WindowManager.LayoutParams.TYPE_TOAST;        params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//让悬浮窗口失去焦点。        params.width = WindowManager.LayoutParams.WRAP_CONTENT;        params.height = WindowManager.LayoutParams.WRAP_CONTENT;        params.format = PixelFormat.RGBA_8888;//背景透明        view = LayoutInflater.from(context).inflate(R.layout.hint_phone_layout, null);        TextView phoneText = (TextView) view.findViewById(R.id.phone_tv);        TextView companyText = (TextView) view.findViewById(R.id.company_text);        TextView inforText = (TextView) view.findViewById(R.id.infor_text);        if (!StringsUtil.isEmpty(phone)) {            phoneText.setText(phone);        }        if (!StringsUtil.isEmpty(companyName)) {            companyText.setText(companyName);        }        if (!StringsUtil.isEmpty(infor)) {            inforText.setText(infor);        }        wm.addView(view, params);    }    private final class PhoneListener extends PhoneStateListener {        private Context context;           public PhoneListener(Context context) {            this.context = context;        }        @Override        public void onCallStateChanged(int state, String incomingNumber) {            try {                switch (state) {                    case TelephonyManager.CALL_STATE_RINGING:   //来电                        Log.d("来电话了", incomingNumber);                                              showWindow(incomingNumber, "name", "infor", context);                        break;                    case TelephonyManager.CALL_STATE_OFFHOOK:   //接通电话                    case TelephonyManager.CALL_STATE_IDLE:  //挂掉电话                        if (wm != null) {                            if (view != null) {                                wm.removeView(view);                                view = null;                            }                        }                        break;                }            } catch (IllegalStateException e) {                e.printStackTrace();            }        }    }}

WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//让悬浮窗口失去焦点。防止来点界面失去焦点,而不能操作。

然后在清单文件中设置开机重启:

<intent-filter>    <action android:name="android.intent.action.BOOT_COMPLETED" /></intent-filter>

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

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

OK,运行一下没问题。

当然也可以开一个service,在后台监听

悬浮窗在手机上是需要权限的,没有权限是不会显示出来的。

在设置type值为 WindowManager.LayoutParams.TYPE_PHONE 和 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT 需要申请 android.permission.SYSTEM_ALERT_WINDOW 权限

type 值为 WindowManager.LayoutParams.TYPE_TOAST 显示的 System overlay view 不需要权限,但仅在 API level >= 19 时可以达到目的,在<19的时候无法接收触摸事件。