android studio 悬浮窗
时间: 2023-04-21 09:02:35 浏览: 494
Android Studio悬浮窗是指在Android应用程序中创建一个浮动的窗口,可以在应用程序的任何位置显示。悬浮窗可以用于显示通知、提醒、快捷操作等功能。在Android Studio中,可以使用WindowManager类来创建和管理悬浮窗。需要注意的是,悬浮窗需要申请SYSTEM_ALERT_WINDOW权限才能正常显示。
相关问题
android studio悬浮窗
在Android Studio中实现悬浮窗可以通过创建一个Service来实现。下面是一个简单的示例代码:
```java
import android.app.Service;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
public class FloatingWindowService extends Service {
private WindowManager windowManager;
private LinearLayout floatingLayout;
@Override
public void onCreate() {
super.onCreate();
// 创建悬浮窗布局
floatingLayout = new LinearLayout(this);
floatingLayout.setBackgroundColor(Color.TRANSPARENT);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
floatingLayout.setLayoutParams(layoutParams);
// 创建悬浮窗按钮
Button floatingButton = new Button(this);
floatingButton.setText("悬浮按钮");
floatingButton.setBackgroundColor(Color.BLUE);
floatingButton.setTextColor(Color.WHITE);
floatingButton.setAlpha(0.7f);
floatingButton.setOnTouchListener(new FloatingOnTouchListener());
floatingLayout.addView(floatingButton);
// 设置悬浮窗参数
WindowManager.LayoutParams params;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
} else {
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
}
params.gravity = Gravity.TOP | Gravity.START;
params.x = 0;
params.y = 100;
// 添加悬浮窗到窗口管理器
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(floatingLayout, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if (floatingLayout != null) {
windowManager.removeView(floatingLayout);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private class FloatingOnTouchListener implements View.OnTouchListener {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = windowManagerParams.x;
initialY = windowManagerParams.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
int newX = initialX + (int) (event.getRawX() - initialTouchX);
int newY = initialY + (int) (event.getRawY() - initialTouchY);
windowManagerParams.x = newX;
windowManagerParams.y = newY;
windowManager.updateViewLayout(floatingLayout, windowManagerParams);
return true;
}
return false;
}
}
}
```
请注意,为了在Android 8.0及更高版本上正常工作,需要在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
```
此外,还需要在AndroidManifest.xml文件中注册Service:
```xml
<service android:name=".FloatingWindowService" />
```
请确保在使用悬浮窗时遵守相关的隐私和安全规定。
Android Studio 悬浮窗
Android Studio悬浮窗是一个在Android Studio中可以直接运行的悬浮窗演示。它通过启动一个Service,并在桌面上绘制一个View来实现。该悬浮窗Demo可以在AndroidManifest.xml文件中的<service>标签中进行配置。同时,在MainActivity的代码中,通过判断设备的SDK版本来决定是否需要获取绘制悬浮窗的权限。
阅读全文