Android floating window, android floating window

Source: Internet
Author: User

Android floating window, android floating window

FloatService:

Package com. home. floatwindow; import android. app. service; import android. content. context; import android. content. intent; import android. graphics. pixelFormat; import android. OS. IBinder; import android. util. log; import android. view. gravity; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. view. onTouchListener; import android. view. windowManag Er; import android. view. windowManager. layoutParams; import android. widget. imageView; import android. widget. linearLayout; import com. home. testfolatwindow. r; public class FloatService extends Service {private WindowManager wm; private WindowManager. layoutParams wmlp; private LinearLayout layout; @ Overridepublic IBinder onBind (Intent intent) {return null ;}@ Overridepublic void onCreate () {super. onCreate (); CRES AteFloatView ();} private void createFloatView () {wm = (WindowManager) getSystemService (Context. WINDOW_SERVICE); wmlp = new WindowManager. layoutParams (); wmlp. type = LayoutParams. TYPE_PHONE; // set the image format. The effect is the background transparent wmlp. format = PixelFormat. RGBA_8888; // set the floating window to not focus on wmlp. flags = LayoutParams. FLAG_NOT_FOCUSABLE; wmlp. gravity = Gravity. LEFT | Gravity. TOP; wmlp. x = 0; wmlp. y = 0; wmlp. width = WindowManager. layoutParams. WRAP_CONTENT; wmlp. height = WindowManager. layoutParams. WRAP_CONTENT; layout = getLayout (); wm. addView (layout, wmlp); layout. measure (View. measureSpec. makeMeasureSpec (0, View. measureSpec. UNSPECIFIED), View. measureSpec. makeMeasureSpec (0, View. measureSpec. UNSPECIFIED); ImageView iv = (ImageView) layout. findViewById (1); iv. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Log. I ("OnClick", "onClick") ;}}); iv. setOnTouchListener (new OnTouchListener () {@ Overridepublic boolean onTouch (View v, MotionEvent event) {// getRawX is the coordinate of the touch position relative to the screen, and getX is the coordinate wmlp relative to the button. x = (int) event. getRawX ()-layout. getMeasuredWidth ()/2; // 25 is the height of the status bar wmlp. y = (int) event. getRawY ()-layout. getMeasuredHeight ()/2-25; wm. updateViewLayout (layout, wmlp); return false ;}}) ;}private LinearLayout getLayout () {L InearLayout layout = new LinearLayout (this); ImageView iv = new ImageView (this); iv. setBackgroundResource (R. drawable. ic_launcher); iv. setId (1); layout. addView (iv); return layout ;}@ Overridepublic void onDestroy () {super. onDestroy (); if (layout! = Null) {// remove the suspended window wm. removeView (layout );}}}


Permission:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />


 


How can I set the priority of an android floating window to hover the self-written floating window over all floating windows and drop-down menus?

If it is set to phone type, it can always be at the top, because the call level is higher than anything else.

This. getWindow (). setType (WindowManager. LayoutParams. TYPE_PHONE );

How does one implement countdown in the android floating window?

Package yzy. yyy. yy;

Import java. util. Timer;
Import java. util. TimerTask;

Import android. app. Activity;
Import android. app. AlertDialog;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. widget. Toast;

Public class TimeToast extends Activity {
Int I = 60;
Handler handler = new Handler (){

@ Override
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
If (msg. what <0 ){
Dialog. dismiss ();
Timer. cancel ();
}
Dialog. setMessage (msg. what + "");
}
};
AlertDialog dialog = null;
Timer timer = new Timer ();
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. ttt );
Dialog = new AlertDialog. Builder (this). setMessage (""). create ();
Dialog. show ();
Timer. schedule (new TimerTask (){

@ Override
Public void run (){
Message msg = handler. obtainMessage ();
Msg. what = I;
Handler. sendEmptyMessage (msg. what );
I = I-1;
}
}, 1000,100 0 );
}
@ Override
Protected void onDestroy (){
Super. onDestroy ();

If (timer! = Null ){
Timer. cancel ();
Timer = null;
}
}
}... Remaining full text>

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.