想要监听来电首先需要要manifest中申明"android.permission.READ_PHONE_STATE"权限
- <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
还需要注册来电监听,目前我的处理方式是接收开机广播,然后在接收到广播后注册来电监听。接收开机广播需要有“android.permission.RECEIVE_BOOT_COMPLETED”权限,manifest中申明如下
- "android.permission.RECEIVE_BOOT_COMPLETED"/>
然后注册广播接收类
- <receiver android:name=".PhoneBootReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- intent-filter>
- receiver>
PhoneBootReceiver中注册监听来电,首先得获取系统服务“TELEPHONY_SERVICE”
- TelephonyManager telM = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
然后添加监听
- telM.listen(new TelListener(context), PhoneStateListener.LISTEN_CALL_STATE);
TelListener是自己定义的电话状态监听类,继承自PhoneStateListener,监听来电只需要实现onCallStateChanged(int state, String incomingNumber)方法。
咳咳...标题上说了弹出悬浮窗口,其实悬浮窗口就是在WindowManager中添加一个View,这个功能我也是在TelListener实现的。要想实现悬浮窗口,首先得有“android.permission.SYSTEM_ALERT_WINDOW”的权限,在manifest中申明如下:
- <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
WindowManager需要通过context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
来获取。
先把TelListener源码放出来,再详解
- public class TelListener extends PhoneStateListener {
-
-
- private Context context;
- private WindowManager wm;
- private TextView tv;
- public TelListener(Context context){
- this.context = context;
- }
-
- @Override
- public void onCallStateChanged(int state, String incomingNumber) {
- // TODO Auto-generated method stub
- super.onCallStateChanged(state, incomingNumber);
- if(state == TelephonyManager.CALL_STATE_RINGING){
-
- wm = (WindowManager)context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
- WindowManager.LayoutParams params = new WindowManager.LayoutParams();
- params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
- 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;
- <span style="white-space: pre;"> span>params.format = PixelFormat.RGBA_8888;
- tv = new TextView(context);
- tv.setText("这是悬浮窗口,来电号码:" + incomingNumber);
- wm.addView(tv, params);
-
- }else if(state == TelephonyManager.CALL_STATE_IDLE){
- if(wm != null){
- wm.removeView(tv);
- }
- }
- }
- }
state = TelephonyManager.CALL_STATE_RINGING表示有新的来电,state = TelephonyManager.CALL_STATE_IDLE表示电话中断(可能理解不是很准确,电话挂断的时候state会和TelephonyManager.CALL_STATE_IDLE相等)
定义窗口布局
- WindowManager.LayoutParams params = new WindowManager.LayoutParams();
设置窗口类型在所有窗口之上
- params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
别忘了
- params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
如果没有这句话的话,在生成悬浮窗口后,悬浮窗口后的界面上东西都不能点。这句话的目的是让悬浮窗口失去焦点。
背景透明
- params.format = PixelFormat.RGBA_8888;
本例中悬浮窗口只是显示一个TextView其内容为“这是悬浮窗口,来电号码:xxxxxx”,最后将TextView添加到窗体中
在电话中断后将TextView移除,否则会一直显示的...
啦..本文就到这儿了...
“啥?要可移动的?”
要想可以拖动的话,那给TextView添加setOnTouchListener,实现OnTouchListener的onTouchListener方法。
对了,别忘了将
- params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
修改为
- params.type = WindowManager.LayoutParams.TYPE_PHONE;
因为TYPE_SYSTEM_OVERLAY的话是TextView获取不到输入焦点,也就没法拖动了哈。
阅读(1334) | 评论(0) | 转发(0) |