android开发,监听来电显示求解决!!!!!
最近在做一个关于来电显示的监控,要实现,在有电话打来时,跳出一个悬浮框用来显示自己的一些提示信息,监控可以成功(监控不在Activity里面,是自定义方法通过广播实现),但悬浮框无法实现,怎么才能出来一个悬浮框,???求大神指导。。。。
public class TeleListenerReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("show", "加载广播");
TelephonyManager mTelePhonyMgr=(TelephonyManager)context.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
//获取系统服务
Log.e("show", "加载广播...");
mTelePhonyMgr.listen(new TeleListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
}
===============================================================
public class TeleListener extends PhoneStateListener {
private static String IMSI;
private Context context;
private WindowManager wm;
// private TextView tv;
LinearLayout mFloat;
LayoutInflater minflater;
public TeleListener(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
Log.e("show", "通话监听");
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.e("show", "来点电话" + incomingNumber);
// 通话结束移除弹框
popPhoneRemover();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e("show", "电话刮起");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.e("show", "来点电话" + incomingNumber);
popPHone(incomingNumber);
break;
default:
break;
}
}
private void popPHone(String phone) {
wm = (WindowManager) context.getApplicationContext().getSystemService(
Context.WINDOW_SERVICE);
minflater = (LayoutInflater) context.getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// minflater=LayoutInflater.from((Context)
// context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE));
mFloat = (LinearLayout) minflater.inflate(R.layout.float_window, null);
// tv=(TextView) mFloat.findViewById(R.id.show);
// tv.setText(phone);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_PHONE;
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;
// wm.addView(mFloat, (android.view.ViewGroup.LayoutParams) wm);
wm.addView(mFloat, (LayoutParams) wm);
}
private void popPhoneRemover() {
wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (wm != null) {
wm.removeView(mFloat);
}
}
}