安卓开发实现悬浮窗显示全局显示,通过悬浮窗实时监控当前流量
- 1 实现效果
- 2 参考学习
- 3 实现说明
- 4 项目代码
- 5 下载地址
1 实现效果
2 参考学习
3 实现说明
1.悬浮窗的显示需要再服务中进行,否则无法对悬浮窗进行操作(测试只可以点击,其它就不行了,可能是线程问题)
2.悬浮窗需要考虑:Android 6.0之后的悬浮窗动态申请和
Window 的ype属性在Android8.0前后的适配
本次采用的是全局的悬浮窗开发,局部悬浮窗开发需要适配不同的手机进行,比较复杂.全局适配可以实现部分局部悬浮窗的功能,但是对于任务切换不能实现,(局部悬浮窗,任务切换的时候会显示,但是全局悬浮窗就不能实现)
3.安卓手机有一般权限,安全权限,和特殊权限(该项目使用了特殊权限的该应用可以至于其他应用上层)—>微信视频的时候默认必须打开这个权限,但是拼多多商城商品界面右上角直播的显示不打开也能实现,估计就是适配了任何app的局部悬浮窗实现的.
4.网络上面很多代码都跑不了添加addView的时候总是报错,估计是安卓系统版本和手机适配不一样,导致大家实现的功能不通用.
5.本代码,悬浮窗显示的时候通过设置了按钮点击的时长来判断是点击还是滑动.
4 项目代码
package com.example.trafficsate;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
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.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends Activity {
//定义浮动窗口布局
LinearLayout mFloatLayout;
//创建浮动窗口设置布局参数的对象
WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
WindowManager mWindowManager;
private Context mContext;
private Button start;
private Button remove;
private boolean isFloatLayout=false;
private ImageView floatView;
private int x;
private int y;
private Intent serviceIntent;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=MainActivity.this;
start = (Button)findViewById(R.id.addbt);
remove = (Button)findViewById(R.id.removebt);
start.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//Android 6.0之后的悬浮窗动态申请,覆盖显示权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !Settings.canDrawOverlays(getApplicationContext())) {
startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + mContext.getPackageName())));
}
serviceIntent= new Intent(MainActivity.this, FxService.class);
startService(serviceIntent);
}
});
remove.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
stopService(serviceIntent);
}
});
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
stopService(serviceIntent);
}
}
2.流量实时监控
package com.example.trafficsate;
import android.content.Context;
import android.net.TrafficStats;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Ricky on 2016/10/13.
*/
public class NetWorkSpeedUtils {
private Context context;
private Handler mHandler;
private long lastTotalRxBytes = 0;
private long lastTimeStamp = 0;
public NetWorkSpeedUtils(Context context, Handler mHandler) {
this.context = context;
this.mHandler = mHandler;
}
TimerTask task = new TimerTask() {
@Override
public void run() {
showNetSpeed();
}
};
public void startShowNetSpeed() {
lastTotalRxBytes = getTotalRxBytes();
lastTimeStamp = System.currentTimeMillis();
new Timer().schedule(task, 1000, 1000); // 1s后启动任务,每2s执行一次
}
private long getTotalRxBytes() {
return TrafficStats.getUidRxBytes(context.getApplicationInfo().uid) == TrafficStats.UNSUPPORTED ? 0 : (TrafficStats.getTotalRxBytes() / 1024);//转为KB
}
private void showNetSpeed() {
long nowTotalRxBytes = getTotalRxBytes();
long nowTimeStamp = System.currentTimeMillis();
long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒转换
long speed2 = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 % (nowTimeStamp - lastTimeStamp));//毫秒转换
lastTimeStamp = nowTimeStamp;
lastTotalRxBytes = nowTotalRxBytes;
Message msg = mHandler.obtainMessage();
msg.what = 100;
msg.obj = getTotalSpeed(speed, speed2);
// msg.obj = String.valueOf(speed) + "." + String.valueOf(speed2) + " kb/s";
mHandler.sendMessage(msg);//更新界面
}
private String getTotalSpeed(long speed, long speed2) {
DecimalFormat showFloatFormat =new DecimalFormat("0.00");
Log.d("速度", "speed: "+speed+",speed2"+speed2);
String totalSpeedStr = "";
if (speed >= 1024) {
totalSpeedStr = showFloatFormat.format(speed/1024)+" M/s";
} else {
totalSpeedStr =showFloatFormat.format(speed+speed2/1024)+"K/s";
}
return totalSpeedStr;
}
}
3.悬浮窗显示服务
package com.example.trafficsate;
import android.Manifest;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.provider.Settings;
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.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.lang.reflect.Method;
public class FxService extends Service {
//定义浮动窗口布局
LinearLayout mFloatLayout;
WindowManager.LayoutParams wmParams;
//创建浮动窗口设置布局参数的对象
WindowManager mWindowManager;
private Context mContext;
TextView mFloatView;
private long startTime;
private long endTime;
private boolean isColor=true;
private static final String TAG = "FxService";
Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
switch (msg.what){
case 100:
mFloatView.setText(msg.obj.toString());
break;
}
super.handleMessage(msg);
}
};
@Override
public void onCreate()
{
super.onCreate();
mContext=FxService.this;
createFloatView();
new NetWorkSpeedUtils(this,handler).startShowNetSpeed();
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
private void createFloatView()
{
wmParams = new WindowManager.LayoutParams();
//获取WindowManagerImpl.CompatModeWrapper
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
//设置window type
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.O){
wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}else{
wmParams.type= WindowManager.LayoutParams.TYPE_TOAST;
}
//设置图片格式,效果为背景透明
wmParams.format = PixelFormat.RGBA_8888;
//设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
wmParams.flags =
// LayoutParams.FLAG_NOT_TOUCH_MODAL |
LayoutParams.FLAG_NOT_FOCUSABLE
// LayoutParams.FLAG_NOT_TOUCHABLE
;
//调整悬浮窗显示的停靠位置为左侧置顶
wmParams.gravity = Gravity.LEFT | Gravity.TOP;
// 以屏幕左上角为原点,设置x、y初始值
wmParams.x = 200;
wmParams.y =200;
//设置悬浮窗口长宽数据
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
LayoutInflater inflater = LayoutInflater.from(getApplication());
//获取浮动窗口视图所在布局
mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null);
mFloatView = (TextView)mFloatLayout.findViewById(R.id.float_id);
mWindowManager.addView(mFloatLayout, wmParams);
// handler.sendEmptyMessage(1);
//浮动窗口按钮
mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//设置监听浮动窗口的触摸移动
mFloatView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean isclick=false;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
startTime=System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
//getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth()/2;
wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight()/2 - 25;
//刷新
mWindowManager.updateViewLayout(mFloatLayout, wmParams);
break;
case MotionEvent.ACTION_UP:
endTime=System.currentTimeMillis();
//小于0.2秒被判断为点击
if ((endTime - startTime) > 200) {
isclick = false;
} else {
isclick = true;
}
break;
}
//响应点击事件
if (isclick) {
if(isColor){
mFloatView.setBackgroundColor(Color.RED);
isColor=!isColor;
}else{
mFloatView.setBackgroundColor(Color.GREEN);
isColor=!isColor;
}
Toast.makeText(mContext, "点击了", Toast.LENGTH_SHORT).show();
}
return true;
}
});
mFloatView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(FxService.this, "onClick", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onDestroy()
{
super.onDestroy();
if(mFloatLayout != null)
{
mWindowManager.removeView(mFloatLayout);
}
}
}
4.权限配置.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trafficsate">
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service
android:name=".FxService"
android:enabled="true"
android:exported="true"></service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
5,2个布局文件.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/addbt"
android:text="加入浮动"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/removebt"
android:text="移除浮动"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<!--------布局文件2-------------->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearlayout"
android:orientation="vertical" >
<TextView
android:id="@+id/float_id"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:background="@color/colorAccent"
android:clickable="true"
android:textSize="8dp"
android:text="浮动显示"/>
</LinearLayout>