Android 监听来去电 弹出悬浮窗提示

在网上找了很多,都是只有来电的悬浮窗。

算了,还是自己研究吧。

下面给出监听部分和悬浮窗,可拖拽的。

//来去电广播
public class InterceptCall extends BroadcastReceiver {
 private static final String TAG = "PhoneStatReceiver";
 private WindowManager wm;
 WindowManager.LayoutParams params;
 private float mTouchStartX;
 private float mTouchStartY;
 private float x;
 private float y;

private Button tv;
 Date curDate = null;

Context mcontext;

private void updateViewPosition() {
  // 更新浮动窗口位置参数
  params.x = (int) (x - mTouchStartX);
  params.y = (int) (y - mTouchStartY);
  wm.updateViewLayout(tv, params);

}

@Override
 public void onReceive(Context context, Intent intent) {
  mcontext = context;
 
  if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
   // 如果是去电(拨出)
   // mCallEvent = CALL_EVENT_OUTGOING;
   MyApplication.NumberType = 1;
   String mCallNumber = intent
     .getStringExtra(Intent.EXTRA_PHONE_NUMBER);
   Log.i(TAG, "call OUT:" + mCallNumber);
   MyApplication.PhoneNum = mCallNumber;

wm = null;
   wm = (WindowManager) context.getApplicationContext()
     .getSystemService(Context.WINDOW_SERVICE);
   // WindowManager.LayoutParams
   params = new WindowManager.LayoutParams();
   
   params.type = LayoutParams.TYPE_PHONE;
   

// 设置Window flag
   params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
     | LayoutParams.FLAG_NOT_FOCUSABLE;
   params.gravity = Gravity.LEFT | Gravity.TOP; // 调整悬浮窗口至左上角 <span>为实现拖拽,必须加上这个,不然会漂移,不信你试试。</span>
   // 以屏幕左上角为原点,设置x、y初始
   params.x = 20;
   params.y = 5;
   params.width = WindowManager.LayoutParams.WRAP_CONTENT;
   params.height = WindowManager.LayoutParams.WRAP_CONTENT;
   params.format = PixelFormat.RGBA_8888;
   tv = new Button(context);
   
   tv.setText("双击查看:\n" + mCallNumber + "的记录");// +sds);

wm.addView(tv, params);

tv.setOnTouchListener(new OnTouchListener() {

@Override
    public boolean onTouch(View v, MotionEvent event) {

// 获取相对屏幕的坐标,即以屏幕左上角为原点
     x = event.getRawX();
     y = event.getRawY() - 25; // 25是系统状态栏的高度

switch (event.getAction()) {
     case MotionEvent.ACTION_DOWN:
      // 获取相对View的坐标,即以此View左上角为原点
      mTouchStartX = event.getX();
      mTouchStartY = event.getY();

break;
     case MotionEvent.ACTION_MOVE:
      updateViewPosition();

break;
     // 判断手势点击起来的时候:
     case MotionEvent.ACTION_UP:
      updateViewPosition();
      mTouchStartX = mTouchStartY = 0;

if (curDate != null) {
       Date dateNow = new Date(System.currentTimeMillis());
       // date转成毫秒
       long diff = dateNow.getTime() - curDate.getTime();

// 这样得到的差值是微秒级别

if (diff < 400) {
        // 双击
        try {
         if (wm != null) {
          wm.removeView(tv);

}
        } catch (Exception e) {

}

curDate = null;

Intent service = new Intent();
        service.setClass(mcontext, TeleLogContent.class);
        service.putExtra("PhoneNumer",
          MyApplication.PhoneNum);
        service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mcontext.startActivity(service);

} else {
        // 单击

curDate = new Date(System.currentTimeMillis());
       }
       // 这里也是单击
      } else {
       // 单击

curDate = new Date(System.currentTimeMillis());
      }

break;
     }
     return true;
    }
   });
   // AskCallStateThread callStateThread = new
   // AskCallStateThread(context);
   // callStateThread.start();

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ac66a16d3728dc033f7e14519afaf8af.html