Android實(shí)現(xiàn)懸浮窗全系統(tǒng)版本
懸浮窗是在系統(tǒng)上顯示的內(nèi)容,好像微信視頻聊天時(shí)的小窗口一樣,在退出軟件后依然存在的一個(gè)窗口,本博客以窗口中放一個(gè)button組件為例,簡單展示懸浮窗,其中包括了對Android 6.0以下、Android 6.0到Android 8.0、Android 8.0以上版本的處理,下面開始介紹實(shí)現(xiàn)方法:
1、MainActivity中的代碼
public Button mFloatingButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化view initView(); } //初始化view private void initView() { mFloatingButton=(Button) findViewById(R.id.floating_btn); mFloatingButton.setOnClickListener(this); } public void startFloatingButtonService(View view) { Log.e("測試流程", "測試流程"); if (FloatingService_Button.isStarted) { Log.e("測試流程2", "測試流程2"); return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//判斷系統(tǒng)版本 if (!Settings.canDrawOverlays(this)) { Toast.makeText(this, "當(dāng)前無權(quán)限,請授權(quán)", Toast.LENGTH_SHORT); Log.e("測試流程3", "測試流程3"); startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0); } else { Log.e("測試流程4", "測試流程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("測試流程5", "測試流程5"); Toast.makeText(this, "授權(quán)失敗", Toast.LENGTH_SHORT).show(); } else { Log.e("測試流程6", "測試流程6"); Toast.makeText(this, "授權(quán)成功", 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; } }
思路簡單解釋:點(diǎn)擊彈出懸浮窗按鈕時(shí),獲取版本并判斷“Build.VERSION.SDK_INT >= Build.VERSION_CODES.M”如果系統(tǒng)版本在6.0以下這不需要請求權(quán)限,如果系統(tǒng)版本在6.0以上需要進(jìn)行權(quán)限檢測以及請求,獲取權(quán)限后,彈出懸浮框
2、activity_main.xml代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.jack_lin.suspendwindow.MainActivity"> <Button android:id="@+id/floating_btn" style="@style/floatingBtn" android:text="@string/floating_btn" /> </LinearLayout>
簡單解釋:xml中沒什么特別東西,線性布局中放一個(gè)按鈕
3、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("進(jìn)入服務(wù)1", "進(jìn)入服務(wù)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("進(jìn)入服務(wù)2", "進(jìn)入服務(wù)2"); return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e("進(jìn)入服務(wù)3", "進(jìn)入服務(wù)3"); showFloatingWindow(); return super.onStartCommand(intent, flags, startId); } private void showFloatingWindow() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//判斷系統(tǒng)版本 if (Settings.canDrawOverlays(this)) { button = new Button(getApplicationContext()); button.setText("我是個(gè)button窗口"); button.setBackgroundColor(Color.BLUE); windowManager.addView(button, layoutParams); button.setOnTouchListener(new FloatingOnTouchListener()); } } else { button = new Button(getApplicationContext()); button.setText("我是個(gè)button窗口"); 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; } }
思路簡單解釋:首先獲取WindowManager服務(wù),然后定義并設(shè)置在window上顯示的layoutParams(此處需注意Android 8.0以上版本中LayoutParam里的type變量變?yōu)門YPE_APPLICATION_OVERLAY與Android 8.0以下版本LayoutParam里的type變量TYPE_PHONE不一樣,需要通過判斷系統(tǒng)版本進(jìn)行區(qū)分),然后定義并設(shè)置在layoutParams上面顯示的Button按鈕以及監(jiān)聽事件(此處的監(jiān)聽事件主要是懸浮窗口拖動(dòng)的監(jiān)聽)最后將設(shè)置好的button與layoutParams添加入window中
4、AndroidManifest.xml中權(quán)限添加
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.INTERNET" />
5、效果圖
源碼下載地址:Android懸浮窗 源碼下載
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android使用Ultra-PullToRefresh實(shí)現(xiàn)下拉刷新自定義代碼
本篇文章主要介紹了android使用Ultra-PullToRefresh實(shí)現(xiàn)下拉刷新新自定義,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02JetPack Compose底部導(dǎo)航欄的實(shí)現(xiàn)方法詳解
開發(fā)一個(gè)新項(xiàng)目,底部導(dǎo)航欄一般是首頁的標(biāo)配,在以前的xml布局中,我們可以很輕松的是用谷歌提供的BottomNavigationView或者自定義來實(shí)現(xiàn)底部導(dǎo)航的功能,在Compose中也有也提供了一個(gè)類似的控件androidx.compose.material.BottomNavigation2022-09-09android重力感應(yīng)開發(fā)之微信搖一搖功能
這篇文章主要為大家詳細(xì)介紹了android重力感應(yīng)開發(fā)之微信搖一搖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05簡單實(shí)現(xiàn)Android滾動(dòng)公告欄
這篇文章主要為大家詳細(xì)介紹了如何簡單實(shí)現(xiàn)Android滾動(dòng)公告欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android 中ViewPager中使用WebView的注意事項(xiàng)
這篇文章主要介紹了Android 中ViewPager中使用WebView的注意事項(xiàng)的相關(guān)資料,希望通過本文大家在使用過程中遇到這樣的問題解決,需要的朋友可以參考下2017-09-09