在前面的文章中 Unity3D研究院之实时获取手机电流、电压、计算功率发热(一百一十八)
我们可以给自己的游戏添加电流功耗显示,但是有时需要对比其他游戏,看看到底是它发热还是自己的游戏发热。如果只是统计别人游戏帧率那么可以很方便使用腾通讯性能狗这样的工具,但是如果帧率在相等情况下就只能根据电流来确认到底谁更发热。
如下图所示,左上角蓝框中实时显示电流,每秒实时刷新一下电流显示。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!Settings.canDrawOverlays(this)) { Toast.makeText(this, "当前无权限,请授权", Toast.LENGTH_SHORT); startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0); }else{ startService(new Intent(MainActivity.this, FloatingButtonService.class)); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (!Settings.canDrawOverlays(this)) { Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show(); startService(new Intent(MainActivity.this, FloatingButtonService.class)); } } } |
主要是FloatingButtonService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
public class FloatingButtonService extends Service { private WindowManager windowManager; private WindowManager.LayoutParams layoutParams; private Button button; BatteryManager bm; String value=""; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { handler.removeMessages(0); float a = ToMA(bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)); value="电流" + a + "毫安"; button.setText("电流" + a + "毫安"); handler.sendEmptyMessageDelayed(0, 1000); }; }; static float ToMA(float maOrua) { return maOrua < 10000 ? maOrua : maOrua / 1000f; } @Override public void onCreate() { super.onCreate(); 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.CENTER; 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; bm = (BatteryManager)getSystemService(BATTERY_SERVICE); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { showFloatingWindow(); return super.onStartCommand(intent, flags, startId); } private void showFloatingWindow() { if (Settings.canDrawOverlays(this)) { button = new Button(getApplicationContext()); button.setBackgroundColor(Color.BLUE); button.setAlpha(0.8f); button.setPadding(0,0,0,0); windowManager.addView(button, layoutParams); handler.sendEmptyMessage(0); 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; } } } |
在AndroidMainifest.xml中写入service权限。
<service android:name=”.FloatingButtonService”></service>
<uses-permission android:name=”android.permission.SYSTEM_ALERT_WINDOW” />
<uses-permission android:name=”android.permission.INTERNET” />
手机安装apk后,打开并且开启悬浮权限就OK了。
安卓悬浮框代码参考 Android悬浮窗的实现
- 本文固定链接: https://www.xuanyusong.com/archives/4832
- 转载请注明: 雨松MOMO 于 雨松MOMO程序研究院 发表
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!
大神,你的技能往奇怪的地方发展了
都是被性能优化闹的