标签:class one long 源码 蓝色 action canvas ttext pos
public class FloatWindowService extends Service { /** * 用于在线程中创建或移除悬浮窗。 */ private Handler handler = new Handler(); private FloatManager floatManager; private Runnable runnable; @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { floatManager = new FloatManager(getApplicationContext()); //每隔1秒会更新一次悬浮窗状态 runnable = new Runnable() { @Override public void run() { boolean isHome = isHome(); floatManager.trigger(isHome); handler.postDelayed(this, 1000); } }; handler.post(runnable); return super.onStartCommand(intent, flags, startId); } /** * 判断当前界面是否是桌面 */ private boolean isHome() { ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1); return getHomes().contains(rti.get(0).topActivity.getPackageName()); } /** * 获得属于桌面的应用的应用包名称 * * @return 返回包含所有包名的字符串列表 */ private List<String> getHomes() { List<String> names = new ArrayList<String>(); PackageManager packageManager = this.getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo ri : resolveInfo) { names.add(ri.activityInfo.packageName); } return names; } /** * 服务销毁时清除任务 */ @Override public void onDestroy() { super.onDestroy(); handler.removeCallbacks(runnable); floatManager.removeWindow(getApplicationContext()); } }该悬浮窗只有在桌面才会出现,当打开其他应用的时候会隐藏。我这里用了Handler去每一秒执行一次判断悬浮窗的状态,式显示还是隐藏。注意:在Service的onDestory()的方法中一定要执行handler.removeCallbacks(),否则不能关闭悬浮窗。
@Override public boolean onTouchEvent(MotionEvent event) { try { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 手指按下时记录必要数据,纵坐标的值都需要减去状态栏高度 xInView = event.getX(); yInView = event.getY(); xDownInScreen = event.getRawX(); yDownInScreen = event.getRawY() - getStatusBarHeight(); xInScreen = event.getRawX(); yInScreen = event.getRawY() - getStatusBarHeight(); break; case MotionEvent.ACTION_MOVE: xInScreen = event.getRawX(); yInScreen = event.getRawY() - getStatusBarHeight(); // 手指移动的时候更新悬浮窗的位置 updateViewPosition(); break; case MotionEvent.ACTION_UP: // 如果手指离开屏幕时,xDownInScreen和xInScreen相等,且yDownInScreen和yInScreen相等,则视为触发了单击事件。 if (xDownInScreen == xInScreen && yDownInScreen == yInScreen) { Intent intent = new Intent(mContext, CleanActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(intent); } break; default: break; } } catch (Exception e) { } return true; }这里我创建了一个管理悬浮窗的类,用来获取悬浮窗的显示状态,控制悬浮窗的显示和移除,并在悬浮小球上显示当前内存使用量,同样也用到了Handler类。代码如下:
public class FloatManager { private FloatView floatView; private LayoutParams floatParams; private WindowManager windowManager; private Context context; private Handler handler = new Handler(); private boolean isFirst = true; private long currentTime; public FloatManager(Context context) { this.context = context; } /** * 判断悬浮窗状态 */ public void trigger(boolean isHome) { try { if (isHome && !isShowing()) { handler.post(createRunnable); handler.post(updateRunnable); if (isFirst) { handler.post(alertRunnable); currentTime = System.currentTimeMillis(); isFirst = false; } } else if (!isHome && isShowing()) { handler.post(destroyRunnable); } else if (isHome && isShowing()) { handler.removeCallbacks(updateRunnable); handler.post(updateRunnable); } } catch (Exception e) { e.printStackTrace(); } } private Runnable createRunnable = new Runnable() { @Override public void run() { try { createWindow(context); } catch (Exception e) { } } }; private Runnable destroyRunnable = new Runnable() { @Override public void run() { try { handler.removeCallbacks(updateRunnable); removeWindow(context); } catch (Exception e) { } } }; private Runnable updateRunnable = new Runnable() { @Override public void run() { try { updateUsedPercent(context); } catch (Exception e) { } } }; /** * 每1秒判断一次,是否满足内存占用大于80%,时间间隔至少30分钟,且在桌面,则弹出吐司提示用户清理 */ private Runnable alertRunnable = new Runnable() { @Override public void run() { try { long tmp = System.currentTimeMillis(); long time = tmp - currentTime; if (MemoryManager.getUsedPercentValue(context) >= 80 && time >= 1000 * 60 * 30 && isShowing()) { Toast.makeText(context, "Mobile phone need to clean up!", Toast.LENGTH_LONG).show(); currentTime = tmp; } handler.postDelayed(alertRunnable, 1000); } catch (Exception e) { } } }; /** * 创建一个悬浮窗。初始位置为屏幕的右部中间位置 */ public void createWindow(Context context) { try { WindowManager windowManager = getWindowManager(context); int screenWidth = windowManager.getDefaultDisplay().getWidth(); int screenHeight = windowManager.getDefaultDisplay().getHeight(); if (floatView == null) { floatView = new FloatView(context); if (floatParams == null) { floatParams = new LayoutParams(); floatParams.type = LayoutParams.TYPE_PHONE; floatParams.format = PixelFormat.RGBA_8888; floatParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE; floatParams.gravity = Gravity.LEFT | Gravity.TOP; floatParams.width = FloatView.viewWidth; floatParams.height = FloatView.viewHeight; floatParams.x = screenWidth; floatParams.y = screenHeight / 2; } floatView.setParams(floatParams); windowManager.addView(floatView, floatParams); } } catch (Exception e) { } } /** * 将悬浮窗从屏幕上移除 */ public void removeWindow(Context context) { try { if (floatView != null) { WindowManager windowManager = getWindowManager(context); windowManager.removeView(floatView); floatView = null; } } catch (Exception e) { } } /** * 更新悬浮窗的TextView上的数据,显示内存使用的百分比。 */ public void updateUsedPercent(Context context) { try { if (floatView != null) { TextView percentView = (TextView) floatView.findViewById(R.id.float_percent); percentView.setText(MemoryManager.getUsedPercentValue(context) + "%"); } } catch (Exception e) { } } /** * 是否有悬浮窗显示在屏幕上。 */ public boolean isShowing() { return floatView != null; } private WindowManager getWindowManager(Context context) { try { if (windowManager == null) { windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); } } catch (Exception e) { } return windowManager; } }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); try { canvas.drawPath(blowWavePath, blowWavePaint); canvas.drawPath(aboveWavePath, aboveWavePaint); Paint mPaint = new Paint(); mPaint.setColor(Color.rgb(64, 64, 64)); mPaint.setAntiAlias(true);// 抗锯齿 // 绘制空心圆矩形 canvas.save(); Path path = new Path(); path.reset(); path.setFillType(Path.FillType.EVEN_ODD); mPaint.setStyle(Paint.Style.FILL); RectF rectF = new RectF(); rectF.set(0, 0, getWidth(), getHeight()); path.addOval(rectF, Path.Direction.CCW); rectF.set(0, 0, getHeight(), getBottom()); path.addRoundRect(rectF, 0, 0, Path.Direction.CW); canvas.drawPath(path, mPaint); canvas.restore(); // 定义画笔2 Paint paint2 = new Paint(); // 消除锯齿 paint2.setAntiAlias(true); // 设置画笔的颜色 paint2.setColor(Color.GRAY); paint2.setStrokeWidth(mStokeWidth); paint2.setStyle(Paint.Style.STROKE); // 画一个空心圆 canvas.drawCircle((float) ((getWidth() >> 1)), (float) (getHeight() >> 1), (float) ((getWidth() >> 1) - (paint2.getStrokeWidth()) / 2), paint2); } catch (Exception e) { } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { setMeasuredDimension(measure(widthMeasureSpec, true), measure(heightMeasureSpec, false)); } catch (Exception e) { } } private int measure(int measureSpec, boolean isWidth) { int result = 0; try { int mode = MeasureSpec.getMode(measureSpec); int size = MeasureSpec.getSize(measureSpec); int padding = isWidth ? getPaddingLeft() + getPaddingRight() : getPaddingTop() + getPaddingBottom(); if (mode == MeasureSpec.EXACTLY) { result = size; } else { result = isWidth ? getSuggestedMinimumWidth() : getSuggestedMinimumHeight(); result += padding; if (mode == MeasureSpec.AT_MOST) { if (isWidth) { result = Math.max(result, size); } else { result = Math.min(result, size); } } } } catch (Exception e) { } return result; } /** * 计算波动轨迹 */ private void calculatePath() { try { aboveWavePath.reset(); blowWavePath.reset(); getWaveOffset(); aboveWavePath.moveTo(0, getHeight()); for (float i = 0; x_zoom * i <= getRight() + max_right; i += offset) { aboveWavePath.lineTo((x_zoom * i), (float) (y_zoom * Math.cos(i + aboveOffset)) + waveToTop); } aboveWavePath.lineTo(getRight(), getHeight()); blowWavePath.moveTo(0, getHeight()); for (float i = 0; x_zoom * i <= getRight() + max_right; i += offset) { blowWavePath.lineTo((x_zoom * i), (float) (y_zoom * Math.cos(i + blowOffset)) + waveToTop); } blowWavePath.lineTo(getRight(), getHeight()); } catch (Exception e) { } }效果如下:
/** * 获取当前可用内存 */ private static long getAvailableMemory(Context context) { long ret = 0L; try { ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); manager.getMemoryInfo(memoryInfo); ret = memoryInfo.availMem; } catch (Exception e) { } return ret; } /** * 获取总共内存 */ public static long getTotalMemory() { long totalMemorySize = 0L; try { String dir = "/proc/meminfo"; FileReader fr = new FileReader(dir); BufferedReader br = new BufferedReader(fr, 2048); String memoryLine = br.readLine(); String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:")); br.close(); totalMemorySize = Long.parseLong(subMemoryLine.replaceAll("\\D+", "")) * 1024; } catch (Exception e) { e.printStackTrace(); } return totalMemorySize; } /** * 获取正在运行的进程数 */ public static int getRunningProcess(Context context) { int ret = 0; try { ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ret = manager.getRunningAppProcesses().size(); } catch (Exception e) { } return ret; } /** * 清理内存,返回释放的内存 */ public static long clearMemory(Context context) { long beforeMem = 0L; long afterMem = 0L; try { ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> list = manager.getRunningAppProcesses(); //清理之前的可用内存 beforeMem = getAvailableMemory(context); if (list != null) { for (ActivityManager.RunningAppProcessInfo info : list) { if (info.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE) { for (String pkg : info.pkgList) { Log.d(TAG, "kill package: " + pkg); manager.killBackgroundProcesses(pkg); } } } } //清理之后的可用内存 afterMem = getAvailableMemory(context); } catch (Exception e) { } return afterMem - beforeMem; }清理内存的时候,小球的水面是先下降到0,然后再上升到当前内存使用量的位置,变化的速度可以自己设置,代码我就不贴了,文末会把完整的源码给大家。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.INTERNET" /> <!--移动数据流量--> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_APN_SETTINGS" /> <!--亮度--> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.DEVICE_POWER" /> <!--GPS--> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" /> <!--wifi--> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <!--蓝牙--> <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <!--闪光灯--> <uses-permission android:name="android.permission.CAMERA" /> <!--清理后台程序--> <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" /> <uses-permission android:name="android.permission.GET_TASKS" />
/** * 判断手电筒是否开启 */ public static boolean isLightOpen() { boolean isLightOpen = false; try { if (camera == null) { camera = Camera.open(); } params = camera.getParameters(); isLightOpen = params.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH) ? true : false; } catch (Exception e) { } return isLightOpen; } /** * 打开手电筒 */ public static void openLight(Context context) { try { PackageManager pm = context.getPackageManager(); FeatureInfo[] features = pm.getSystemAvailableFeatures(); for (FeatureInfo f : features) { if (PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) //判断设备是否支持闪光灯 { params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); // 开始亮灯 } } } catch (Exception e) { } } /** * 关闭手电筒 */ public static void closeLight() { try { if (camera != null) { params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(params); camera.stopPreview(); // 关掉亮灯 camera.release(); // 关掉照相机 camera = null; } } catch (Exception e) { } }
//注册监听wifi状态的广播接收者 wifiReceiver = new WifiReceiver(); IntentFilter wififilter = new IntentFilter(); wififilter.setPriority(2147483647); wififilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); registerReceiver(wifiReceiver, wififilter); bt_wifi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { if (mWifiManager.isWifiEnabled()) { mWifiManager.setWifiEnabled(false); } else { mWifiManager.setWifiEnabled(true); } } catch (Exception e) { } } });
/** * 设置数据网络开关 */ public boolean changeNetConnection(Context context, boolean on) { try { if (Build.VERSION.SDK_INT == Build.VERSION_CODES.FROYO) { Method dataConnSwitchmethod; TelephonyManager telephonyManager = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); Class<?> telephonyManagerClass = Class.forName(telephonyManager .getClass().getName()); Method getITelephonyMethod = telephonyManagerClass .getDeclaredMethod("getITelephony"); getITelephonyMethod.setAccessible(true); Object ITelephonyStub = getITelephonyMethod .invoke(telephonyManager); Class<?> ITelephonyClass = Class.forName(ITelephonyStub .getClass().getName()); if (on) { dataConnSwitchmethod = ITelephonyClass .getDeclaredMethod("enableDataConnectivity"); } else { dataConnSwitchmethod = ITelephonyClass .getDeclaredMethod("disableDataConnectivity"); } dataConnSwitchmethod.setAccessible(true); dataConnSwitchmethod.invoke(ITelephonyStub); } else { final ConnectivityManager conman = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); final Class<?> conmanClass = Class.forName(conman.getClass() .getName()); final Field iConnectivityManagerField = conmanClass .getDeclaredField("mService"); iConnectivityManagerField.setAccessible(true); final Object iConnectivityManager = iConnectivityManagerField .get(conman); final Class<?> iConnectivityManagerClass = Class .forName(iConnectivityManager.getClass().getName()); final Method setMobileDataEnabledMethod = iConnectivityManagerClass .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); setMobileDataEnabledMethod.setAccessible(true); setMobileDataEnabledMethod.invoke(iConnectivityManager, on); } return true; } catch (Exception e) { } return false; }
//蓝牙开关 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); updateBluetooth(); //注册监听蓝牙状态的广播接收者 blueToothReceiver = new BlueToothReceiver(); IntentFilter bluefilter = new IntentFilter(); bluefilter.setPriority(2147483647); bluefilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(blueToothReceiver, bluefilter); bt_bluetooth.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { switch (getBluetoothStatus()) { case BluetoothAdapter.STATE_ON: case BluetoothAdapter.STATE_TURNING_ON: mBluetoothAdapter.disable(); break; case BluetoothAdapter.STATE_OFF: case BluetoothAdapter.STATE_TURNING_OFF: mBluetoothAdapter.enable(); break; } } catch (Exception e) { } } });
标签:class one long 源码 蓝色 action canvas ttext pos
原文地址:http://blog.csdn.net/hello_1995/article/details/52984309