Android desktop floating box

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.