Recently, many programs have created a floating box on the desktop to monitor the memory, or display the lyrics or something. The Code is as follows:
@ Override
Public void onStart (Intent intent, int startId ){
Super. onStart (intent, startId );
// Use WindowManager to add a floating window to the screen
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;
// Add an additional permission to use this type
MLayoutParams. type = WindowManager. LayoutParams. TYPE_PHONE;
MWindowManager. addView (mView, mLayoutParams );
ListenActivity ();
}
Private void listenActivity (){
New Thread (){
Public void run (){
List <RunningTaskInfo> taskInfos;
// Determine whether the program is on the desktop
// I found a better method for listening, so I used the most stupid method.
// Check every 100 milliseconds because the requirements are not high
// Leave a message if you have any good methods. Thank you very much.
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 ()){
// Drag www.2cto.com on the desktop
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;
}
The main function is to drag and drop the desktop, hide it automatically under other programs, and respond to some clicks.
From da Xiaosheng