In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to achieve the suspension window full system version of Android, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Suspension window is the content displayed on the system, just like the small window of Wechat video chat, which still exists after exiting the software. This blog takes a button component in the window as an example to simply display the suspension window, including the processing of versions below Android 6.0. Android 6.0to Android 8.0and Android 8.0. here's how to implement it:
1. The code in MainActivity
Public Button mFloatingButton; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / initialize view initView ();} / / initialize view private void initView () {mFloatingButton= (Button) findViewById (R.id.floating_btn); mFloatingButton.setOnClickListener (this);} public void startFloatingButtonService (View view) {Log.e ("Test flow", "Test flow") If (FloatingService_Button.isStarted) {Log.e ("Test flow 2", "Test process 2"); return;} if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.M) {/ / determine the system version if (! Settings.canDrawOverlays (this)) {Toast.makeText (this, "No permission currently, please authorize", Toast.LENGTH_SHORT); Log.e ("Test flow 3", "Test process 3") StartActivityForResult (new Intent (Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse ("package:" + getPackageName ()), 0);} else {Log.e ("Test flow 4", "Test process 4"); startService (new Intent (MainActivity.this, FloatingService_Button.class));}} else {startService (new Intent (MainActivity.this, FloatingService_Button.class)) } @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {if (requestCode = = 0) {if (! Settings.canDrawOverlays (this)) {Log.e ("Test flow 5", "Test process 5"); Toast.makeText (this, "Authorization failure", Toast.LENGTH_SHORT). Show ();} else {Log.e ("Test flow 6", "Test process 6") Toast.makeText (this, "Authorization successful", Toast.LENGTH_SHORT). Show (); startService (new Intent (MainActivity.this, FloatingService_Button.class));} @ Override public void onClick (View v) {switch (v.getId ()) {case R.id.floating_btn: startFloatingButtonService (v); break;}}
Simple explanation of the idea: when you click the pop-up floating window button, get the version and judge "Build.VERSION.SDK_INT > = Build.VERSION_CODES.M" if the system version is less than 6.0. if the system version is below 6.0. if the system version is above 6.0. if you need permission detection and request, after obtaining the permission, the suspension box will pop up.
2. Activity_main.xml code
Simple explanation: there is nothing special in xml, there is a button in the linear layout
3. The code of FloatingService_Button
Public static boolean isStarted = false; private WindowManager windowManager; private WindowManager.LayoutParams layoutParams; private Button button; @ Override public void onCreate () {super.onCreate (); Log.e ("enter Service 1", "enter Service 1"); isStarted = true; windowManager = (WindowManager) getSystemService (WINDOW_SERVICE); layoutParams = new WindowManager.LayoutParams (); if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.O) {layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY } else {layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;} layoutParams.format = PixelFormat.RGBA_8888; layoutParams.gravity = Gravity.LEFT | Gravity.TOP; layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; layoutParams.width = 500; layoutParams.height = 100; layoutParams.x = 300; layoutParams.y = 300;} @ Nullable @ Override public IBinder onBind (Intent intent) {Log.e ("enter Service 2", "enter Service 2"); return null } @ Override public int onStartCommand (Intent intent, int flags, int startId) {Log.e ("enter Service 3", "enter Service 3"); showFloatingWindow (); return super.onStartCommand (intent, flags, startId);} private void showFloatingWindow () {if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.M) {/ / judge system version if (Settings.canDrawOverlays (this)) {button = new Button (getApplicationContext ()); button.setText ("I am a button window") Button.setBackgroundColor (Color.BLUE); windowManager.addView (button, layoutParams); button.setOnTouchListener (new FloatingOnTouchListener ());}} else {button = new Button (getApplicationContext ()); button.setText ("I am a button window"); button.setBackgroundColor (Color.BLUE); windowManager.addView (button, layoutParams); button.setOnTouchListener (new FloatingOnTouchListener ());}} private class FloatingOnTouchListener implements View.OnTouchListener {private int x; private int y Override public boolean onTouch (View view, MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: X = (int) event.getRawX (); y = (int) event.getRawY (); break; case MotionEvent.ACTION_MOVE: int nowX = (int) event.getRawX (); int nowY = (int) event.getRawY (); int movedX = nowX-x; int movedY = nowY-y; x = nowX; y = nowY; layoutParams.x = layoutParams.x + movedX; layoutParams.y = layoutParams.y + movedY WindowManager.updateViewLayout (view, layoutParams); break; default: break;} return false;}}
Simple explanation of the idea: first obtain the WindowManager service, and then define and set the layoutParams displayed on the window (note here that the type variable in LayoutParam in Android version 8.0 or above is different from the type variable TYPE_PHONE in LayoutParam below Android version 8.0, which needs to be distinguished by judging the system version) Then define and set the Button button displayed on the layoutParams and the listening events (the listening events here are mainly those dragged by the floating window). Finally, add the set button and layoutParams to the window.
4. Add permissions in AndroidManifest.xml
Thank you for reading this article carefully. I hope the article "how to realize the suspension window full system version of Android" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.