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
最新资源
- 基于Python实现的VisionTransformer架构设计与源码学习
- 基于Java语言的Arduino开源电子原型平台设计源码
- 基于Java语言的PetShop电商平台设计源码
- 基于Java语言的大学生社团管理系统Server端设计源码
- 基于Java语言的Zzyl-Together合作智慧养老项目设计源码
- 基于Thinkphp5框架的Java插件设计源码
- 基于Python、JavaScript和Vue的“大道无形,生育天地”主题网站设计源码
- 基于Netty4与Spring、MyBatis等流行框架的轻量级RESTful HTTP服务器设计源码
- 基于Jupyter Notebook的Python与Shell脚本分享设计源码
- 基于Java的Android平台Ecg绘图设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0