gpt4 book ai didi

java - 如何创建覆盖窗口以显示 Android 10 上的所有应用程序

转载 作者:行者123 更新时间:2023-12-01 18:11:57 26 4
gpt4 key购买 nike

大家好,我认识的其他人尝试做一些类似的事情,但我能找到的都是旧的,并且似乎不适用于 Android 10。

我想做的实际上是一个看起来有点像某些自定义 ROM 开发选项中的“显示 CPU 统计信息”功能的覆盖层。这就是我的意思:

cpu stats overlay.

我有一个内核应用程序,我想显示一个像这样的叠加层,显示 CPU 和 GPU 频率等内核统计信息。 CPU和GPU频率的代码不是问题。

我实际上想知道如何创建叠加层。这是我迄今为止在网上找到的教程中尝试过的内容,但我尝试的每个教程都会遇到相同的错误:无法添加窗口—— token null 无效;您的 Activity 正在运行吗?

这是我在manifest.xml中使用的内容:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application>...
...
...
<service android:name="org.pierre2324.nogravity.HUD"/>
</application>

这是我的 fragment ,其中包含启用叠加 (HUD) 服务的开关:

public class MainFragment9 extends Fragment {

private static final String TAG = "MainFragment9";
private Switch overandroidlaySwitch;

@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

View view = inflater.inflate(R.layout.main_fragment9, container, false);

overlaySwitch = (Switch) view.findViewById(R.id.overlay_toggle);

//region HUD SWITCH
overlaySwitch.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (overlaySwitch.isChecked()) {
//Start the service
if(!isSystemAlertPermissionGranted(MainFragment9.this.getActivity())){
requestSystemAlertPermission(MainFragment9.this.getActivity(),1);
}
if (Build.VERSION.SDK_INT >= 26) {
MainFragment9.this.getActivity().getApplicationContext().startForegroundService(new Intent(MainFragment9.this.getActivity().getApplicationContext(), HUD.class));
} else {
MainFragment9.this.getActivity().startService(new Intent(MainFragment9.this.getActivity().getApplicationContext(), HUD.class));
}
newEditor.putBoolean("Overlay", true);
Toast.makeText(MainFragment9.this.getActivity() ,"NGK Overlay enabled!", Toast.LENGTH_SHORT).show();
} else {
//Stop the service
MainFragment9.this.getContext().stopService(new Intent(getContext(), HUD.class));
newEditor.putBoolean("Overlay", false);
Toast.makeText(MainFragment9.this.getContext(), "NGK Overlay disabled", Toast.LENGTH_SHORT).show();
}
newEditor.apply();
}
});
//endregion

return view;
}
}

最后这是我的 HUD 服务:

public class HUD extends Service{

private View topLeftView;

private Button overlayedButton;
private WindowManager wm;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();

wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

topLeftView = new View(this);
WindowManager.LayoutParams topLeftParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_TOAST, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT);
topLeftParams.gravity = Gravity.LEFT | Gravity.TOP;
topLeftParams.x = 0;
topLeftParams.y = 0;
topLeftParams.width = 0;
topLeftParams.height = 0;
wm.addView(topLeftView, topLeftParams); //Crashes here...
}

@Override
public void onDestroy() {
super.onDestroy();
if (overlayedButton != null) {
wm.removeView(overlayedButton);
wm.removeView(topLeftView);
overlayedButton = null;
topLeftView = null;
}
}
}

再次感谢您的帮助!

编辑:

我使用覆盖层本身的单独 xml 文件和以下代码使覆盖层正常工作:

public class HUD extends Service{

static final String CHANNEL_ID = "Overlay_notification_channel";

private static final int LayoutParamFlags = WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;

private LayoutInflater inflater;
private Display mDisplay;
private View layoutView;
private WindowManager windowManager;
private WindowManager.LayoutParams params;

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
LayoutParamFlags,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.END;
windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
mDisplay = windowManager.getDefaultDisplay();
inflater = LayoutInflater.from(this);
layoutView = inflater.inflate(R.layout.ngk_overlay, null);
windowManager.addView(layoutView, params);

//This is needed to keep the service running in background just needs a notification to call with startForeground();
if (Build.VERSION.SDK_INT >= 26) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, getString(R.string.ngk_overlay_notification), NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(null, null);
notificationManager.createNotificationChannel(notificationChannel);
Notification.Builder builder = new Notification.Builder(this, CHANNEL_ID);
builder.setContentTitle(getString(R.string.ngk_overlay)).setContentText(getString(R.string.ngk_overlay_notification)).setSmallIcon(R.drawable.ic_mono2);
startForeground(1, builder.build());
}
}

@Override
public void onDestroy() {
super.onDestroy();
windowManager.removeView(layoutView);
}
}

最佳答案

您是否像这样将服务添加到 list 中?

<service android:name="com.xxx.xxx.HUD">
</service>
</application>

或者也许尝试 WindowManager.LayoutParams.TYPE_PHONE (但我认为这仅适用于较旧的 api)。

编辑:另外,尝试让上下文/Activity 有所不同。

MainFragment9.this.getActivity().getApplicationContext() and MainFragment9.this.getActivity()

看起来不太好。在方法外部声明 Activity 对象。更像这样的东西(未经测试的伪代码):

...
private Switch overandroidlaySwitch;
private Activity myActivity;
@Nullable
public View onCreateView
myActivity = getActivity();

关于java - 如何创建覆盖窗口以显示 Android 10 上的所有应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60454766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com