嗨,我认识的其他人都尝试做类似的事情,但是我所能找到的只是古老的,在Android 10上似乎不适用于我。
我实际上想做的是一个看起来有点像某些自定义ROM开发选项中的“显示CPU统计信息”功能的叠加层。这就是我的意思:
我有一个内核应用程序,我想显示一个这样的覆盖图,其中显示了诸如cpu和gpu freqs之类的内核统计信息。 cpu和gpu频率的代码不是问题。
我实际上想知道如何创建覆盖图。这是到目前为止我在网上找到的教程尝试过的东西,但是我尝试的每个教程都遇到相同的错误:Unable to add window -- token null is not valid; is your activity running?
这是我在manifest.xml中使用的:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
这是我的片段,其中包含启用覆盖(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;
}
}
}
再次感谢您的帮助!
您是否像这样将服务添加到清单中?
<service android:name="com.xxx.xxx.HUD">
</service>
</application>
或者也许尝试WindowManager.LayoutParams.TYPE_PHONE(但我认为那只是针对较老的api的。)>