Android桌面懸浮框

最近看好多程序都弄瞭一個桌面的懸浮框,用來監視內存,或者是顯示歌詞什麼的,自己手動實現一個,代碼如下:

@Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
       
        //通過WindowManager將浮動的窗口添加到屏幕
        mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       
        mView = (ViewGroup) mInflater.inflate(R.layout.float_window, null);
        mView.setOnTouchListener(this);
        mView.setVisibility(View.GONE);
        mTextView = (TextView) mView.findViewById(R.id.text);
       
        mLayoutParams = new WindowManager.LayoutParams();
        mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mLayoutParams.alpha = (float) 0.99;
        mLayoutParams.gravity =  Gravity.LEFT | Gravity.TOP;
        mLayoutParams.x = 10;
        mLayoutParams.y = 10;
        mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        //使用此類型要額外添加一個權限
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
       
        mWindowManager.addView(mView, mLayoutParams);
        listenActivity();
    }
   
    private void listenActivity(){
        new Thread(){
            public void run(){
                List<RunningTaskInfo> taskInfos;
                //判斷程序是否處於桌面
                //木有找到比較好的監聽方法,所以用瞭一個最笨的方法
                //因為對相應要求不高,所以每100毫秒檢查一次
                //有好方法的請留言,不勝感謝
                try {
                    while(true){
                        sleep(100);
                        taskInfos = mActivityManager.getRunningTasks (1);
                        if(taskInfos.get(0).topActivity.getPackageName().equals("com.android.launcher") ){
                            mHandler.sendEmptyMessage(View.VISIBLE);
                        }else{
                            mHandler.sendEmptyMessage(View.GONE);
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
   
    @Override
    public void onDestroy() {
        mWindowManager.removeView(mView);
        super.onDestroy();
    }
@Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
       
        //此處實現在桌面的拖動 www.aiwalls.com
        case MotionEvent.ACTION_DOWN:
            xOffset = (int) event.getRawX();
            yOffset = (int) event.getRawY();
            x = mLayoutParams.x;
            y = mLayoutParams.y;
            startTime = System.currentTimeMillis();
            break;
        case MotionEvent.ACTION_MOVE:
            if ( System.currentTimeMillis() – startTime < 1000 )break;
            mLayoutParams.x = x + (int) event.getRawX() – xOffset;
            mLayoutParams.y = y + (int) event.getRawY() – yOffset;
            mWindowManager.updateViewLayout(mView, mLayoutParams);
            break;
        case MotionEvent.ACTION_UP:
            if (robotMsg%2 == 0){
                mTextView.setText("Hello Boy!");
            }else{
                mTextView.setText("Hello Gril!!");
            }
            robotMsg ++;           
            break;
        }
        return true;
    }

 

主要實現的功能就是在桌面拖拽,在其他程序下自動隱藏,響應一些點擊。

 

 

摘自 達小生

發佈留言