Android桌面懸浮框

l_serein發表於2013-05-31

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


@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.2cto.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;
    }

 

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

相關文章