package hq.memFloat.service;
//Download by http://www.codefans.net
import hq.memFloat.R;
import hq.memFloat.model.MyApplication;
import hq.memFloat.model.memInfo;
import java.util.Timer;
import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
public class FloatService extends Service {
WindowManager wm = null;
WindowManager.LayoutParams wmParams = null;
View view;
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
int state;
TextView tx1;
TextView tx;
ImageView iv;
private float StartX;
private float StartY;
int delaytime=1000;
@Override
public void onCreate() {
Log.d("FloatService", "onCreate");
super.onCreate();
view = LayoutInflater.from(this).inflate(R.layout.floating, null);
tx = (TextView) view.findViewById(R.id.memunused);
tx1 = (TextView) view.findViewById(R.id.memtotal);
tx.setText("" + memInfo.getmem_UNUSED(this) + "KB");
tx1.setText("" + memInfo.getmem_TOLAL() + "KB");
iv = (ImageView) view.findViewById(R.id.img2);
iv.setVisibility(View.GONE);
createView();
handler.postDelayed(task, delaytime);
}
private void createView() {
SharedPreferences shared = getSharedPreferences("float_flag",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit();
editor.putInt("float", 1);
editor.commit();
// 获取WindowManager
wm = (WindowManager) getApplicationContext().getSystemService("window");
// 设置LayoutParams(全局变量)相关参数
wmParams = ((MyApplication) getApplication()).getMywmParams();
wmParams.type = 2002;
wmParams.flags |= 8;
wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 调整悬浮窗口至左上角
// 以屏幕左上角为原点,设置x、y初始值
wmParams.x = 0;
wmParams.y = 0;
// 设置悬浮窗口长宽数据
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.format = 1;
wm.addView(view, wmParams);
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// 获取相对屏幕的坐标,即以屏幕左上角为原点
x = event.getRawX();
y = event.getRawY() - 25; // 25是系统状态栏的高度
Log.i("currP", "currX" + x + "====currY" + y);// 调试信息
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
state = MotionEvent.ACTION_DOWN;
StartX = x;
StartY = y;
// 获取相对View的坐标,即以此View左上角为原点
mTouchStartX = event.getX();
mTouchStartY = event.getY();
Log.i("startP", "startX" + mTouchStartX + "====startY"
+ mTouchStartY);// 调试信息
break;
case MotionEvent.ACTION_MOVE:
state = MotionEvent.ACTION_MOVE;
updateViewPosition();
break;
case MotionEvent.ACTION_UP:
state = MotionEvent.ACTION_UP;
updateViewPosition();
showImg();
mTouchStartX = mTouchStartY = 0;
break;
}
return true;
}
});
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent serviceStop = new Intent();
serviceStop.setClass(FloatService.this, FloatService.class);
stopService(serviceStop);
}
});
}
public void showImg() {
if (Math.abs(x - StartX) < 1.5 && Math.abs(y - StartY) < 1.5
&& !iv.isShown()) {
iv.setVisibility(View.VISIBLE);
} else if (iv.isShown()) {
iv.setVisibility(View.GONE);
}
}
private Handler handler = new Handler();
private Runnable task = new Runnable() {
public void run() {
// TODO Auto-generated method stub
dataRefresh();
handler.postDelayed(this, delaytime);
wm.updateViewLayout(view, wmParams);
}
};
public void dataRefresh() {
tx.setText("" + memInfo.getmem_UNUSED(this) + "KB");
tx1.setText("" + memInfo.getmem_TOLAL() + "KB");
}
private void updateViewPosition() {
// 更新浮动窗口位置参数
wmParams.x = (int) (x - mTouchStartX);
wmParams.y = (int) (y - mTouchStartY);
wm.updateViewLayout(view, wmParams);
}
@Override
public void onStart(Intent intent, int startId) {
Log.d("FloatService", "onStart");
setForeground(true);
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
handler.removeCallbacks(task);
Log.d("FloatService", "onDestroy");
wm.removeView(view);
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
weixin_38651540
- 粉丝: 5
- 资源: 914
最新资源
- 连接ESP32手表来做验证20241223-140953.pcapng
- 小偏差线性化模型,航空发动机线性化,非线性系统线性化,求解线性系统具体参数,最小二乘拟合 MATLAB Simulink 航空发动机,非线性,线性,非线性系统,线性系统,最小二乘,拟合,小偏差,系统辨
- 好用的Linux终端管理工具,支持自定义多行脚本命令,密码保存、断链续接,SFTP等功能
- Qt源码ModbusTCP 主机客户端通信程序 基于QT5 QWidget, 实现ModbusTCP 主机客户端通信,支持以下功能: 1、支持断线重连 2、通过INI文件配置自定义服务器I
- Linux下TurboVNC+VirtualGL 使用GPU卡vglrun glxgears
- QGroundControl-installer.exe
- Linux下TurboVNC+VirtualGL 使用GPU卡vglrun glxgears
- 台球检测40-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- 颜色拾取器 for Windows
- 数字按键3.2考试代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0