当前位置:操作系统 > 安卓/Android >>

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

最近给老大做个安卓手机的小程序,希望实现 来去电的时候,提示一下(主要是看看有没有这个号码的相关事情)。

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

算了,还是自己研究吧。

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

 

 

[java]
//来去电广播  
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 style="COLOR: #ff0000">为实现拖拽,必须加上这个,不然会漂移,不信你试试。</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; 
&

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,