http://www.iteye.com/topic/1118711
最近因为项目的原因需要实现一个来电监听,且生成一个悬浮窗口提示相关信息(具体什么信息不方便透露哈)。
现把我的思路及实现方法大致说下哈。
想要监听来电首先需要要manifest中申明"android.permission.READ_PHONE_STATE"权限
- <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
还需要注册来电监听,目前我的处理方式是接收开机广播,然后在接收到广播后注册来电监听。接收开机广播需要有“android.permission.RECEIVE_BOOT_COMPLETED”权限,manifest中申明如下
- <uses-permission android:name="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添加到窗体中
- wm.addView(tv, params);
在电话中断后将TextView移除,否则会一直显示的...
- wm.removeView(tv);
啦..本文就到这儿了...
“啥?要可移动的?”
要想可以拖动的话,那给TextView添加setOnTouchListener,实现OnTouchListener的onTouchListener方法。
对了,别忘了将
- params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
修改为
- params.type = WindowManager.LayoutParams.TYPE_PHONE;
因为TYPE_SYSTEM_OVERLAY的话是TextView获取不到输入焦点,也就没法拖动了哈。
http://blog.csdn.net/l_serein/article/details/7475678
参考:
android 呼入电话的监听(来电监听)
http://stephen830.iteye.com/blog/1181010
android 呼出电话的监听(去电监听)
http://stephen830.iteye.com/blog/1181452
android-轻松监听来电和去电
http://www.eoeandroid.com/thread-8994-1-1.html
android 呼入电话的监听(来电监听)
需要权限:
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
方式一:通过广播接收来电
定义来电广播接收类
- package com.zhouzijing.android.demo;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.telephony.TelephonyManager;
- import android.util.Log;
- public class BroadcastReceiverMgr extends BroadcastReceiver {
- private final String TAG = MyBroadcastReceiver.TAG;
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- Log.i(TAG, "[Broadcast]"+action);
- //呼入电话
- if(action.equals(MyBroadcastReceiver.B_PHONE_STATE)){
- Log.i(TAG, "[Broadcast]PHONE_STATE");
- doReceivePhone(context,intent);
- }
- }
- /**
- * 处理电话广播.
- * @param context
- * @param intent
- */
- public void doReceivePhone(Context context, Intent intent) {
- String phoneNumber = intent.getStringExtra(
- TelephonyManager.EXTRA_INCOMING_NUMBER);
- TelephonyManager telephony =
- (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
- int state = telephony.getCallState();
- switch(state){
- case TelephonyManager.CALL_STATE_RINGING:
- Log.i(TAG, "[Broadcast]等待接电话="+phoneNumber);
- break;
- case TelephonyManager.CALL_STATE_IDLE:
- Log.i(TAG, "[Broadcast]电话挂断="+phoneNumber);
- break;
- case TelephonyManager.CALL_STATE_OFFHOOK:
- Log.i(TAG, "[Broadcast]通话中="+phoneNumber);
- break;
- }
- }
- }
定义Actitvity类
- package com.zhouzijing.android.demo;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.Bundle;
- import android.telephony.PhoneStateListener;
- import android.telephony.TelephonyManager;
- import android.util.Log;
- import android.view.View;
- public class MyBroadcastReceiver extends Activity {