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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android悬浮窗源码,显示监视内容悬浮窗口
共37个文件
class:15个
png:6个
java:5个
需积分: 50 9 下载量 116 浏览量
2021-05-16
00:24:57
上传
评论
收藏 109KB RAR 举报
温馨提示
摘要:Java源码,Android源码,悬浮窗 android悬浮窗的实现源码,可作为参考的一个例子,用于显示被监视内容的悬浮窗口,比如360卫士就有内存使用状况的悬浮窗口,显示在屏幕的某个边缘。悬浮窗在andorid中使用广泛,不过悬浮窗口不要太大,也不要滥用哦,避免用户生厌。 悬浮窗实现说明: 1.悬浮窗可随意移动 2.实时显示当前内存数据 3.上层数据表示可用内存值. 4.下层数据表示总内存值 5.点击悬浮窗出现关闭小图标可直接关闭 在MyApplication类中:创建全局变量,全局变量一般都比较倾向于创建一个单独的数据类文件,并使用static静态变量。
资源详情
资源评论
资源推荐
收起资源包目录
A012548248-22130.rar (37个子文件)
okbase.net
android 悬浮窗 监视内容
memFloat
.project 844B
proguard.cfg 1KB
src
hq
memFloat
service
FloatService.java 5KB
model
MyApplication.java 613B
memInfo.java 1KB
main
memFloat.java 2KB
AndroidManifest.xml 893B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 2KB
drawable
icon.png 2KB
close.png 22KB
meminfo.png 25KB
drawable-mdpi
icon.png 2KB
layout
floating.xml 2KB
main.xml 1KB
values
strings.xml 168B
gen
hq
memFloat
R.java 1KB
default.properties 362B
.classpath 280B
bin
resources.ap_ 16KB
hq
memFloat
R$layout.class 405B
service
FloatService$2.class 2KB
FloatService.class 6KB
FloatService$1.class 1KB
FloatService$3.class 1021B
R$drawable.class 439B
R$attr.class 319B
model
MyApplication.class 650B
memInfo.class 2KB
R$id.class 570B
R.class 482B
R$string.class 406B
main
memFloat.class 2KB
memFloat$1.class 1KB
memFloat$2.class 1KB
memFloat.apk 24KB
classes.dex 12KB
共 37 条
- 1
weixin_38651540
- 粉丝: 5
- 资源: 914
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0