4

Unity3D研究院之悬浮框显示其他游戏电流功耗(一百二十六)

 3 years ago
source link: https://www.xuanyusong.com/archives/4832
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
首页 > Unity3D频道 > 【Unity3D研究院之游戏开发】 > Unity3D研究院之悬浮框显示其他游戏电流功耗(一百二十六)

Unity3D研究院之悬浮框显示其他游戏电流功耗(一百二十六)

在前面的文章中 Unity3D研究院之实时获取手机电流、电压、计算功率发热(一百一十八)

我们可以给自己的游戏添加电流功耗显示,但是有时需要对比其他游戏,看看到底是它发热还是自己的游戏发热。如果只是统计别人游戏帧率那么可以很方便使用腾通讯性能狗这样的工具,但是如果帧率在相等情况下就只能根据电流来确认到底谁更发热。

如下图所示,左上角蓝框中实时显示电流,每秒实时刷新一下电流显示。

Unity3D研究院之悬浮框显示其他游戏电流功耗(一百二十六) - 雨松MOMO程序研究院 - 1

MainActivity.java

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

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悬浮窗的实现

作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

留下一个回复 取消回复

你的email不会被公开。

评论

显示名称 *

电子邮箱地址 *

网站网址

在此浏览器中保存我的显示名称、邮箱地址和网站网址,以便下次评论时使用。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK