手机APP缓存的获取和清理功能的实现
package com.loaderman.appcachedemo; import android.content.pm.IPackageDataObserver;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.view.View;
import android.widget.Button; import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Method; public class MainActivity extends AppCompatActivity { private PackageManager mPM;
private Button btnCache;
private Button btnClean; private long cacheSize; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//模拟缓存
moniCache();
btnCache = (Button) findViewById(R.id.btn_cache);
btnClean = (Button) findViewById(R.id.btn_clean);
mPM = getPackageManager();
} private void moniCache() {
//获取当前缓存路径: data/data/包名/cache
File cacheDir = getCacheDir();
//往缓存里面写点东西模拟
File cacheFile = new File(cacheDir, "cache.txt");
try {
FileOutputStream out = new FileOutputStream(cacheFile);
out.write("jfaklsdjfaklsdjfklasdjfkladsfjlkasdjflkasdflkasdjf".getBytes());
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void startScan(View view) {
SaoMiaoCache();
}
private void SaoMiaoCache() {
try {
Method method = mPM.getClass().getMethod("getPackageSizeInfo",
String.class, IPackageStatsObserver.class);
method.invoke(mPM, getPackageName(), new MyObserver());
} catch (Exception e) {
e.printStackTrace();
}
}
public void cleanCache(View view){
try {
Method method = mPM.getClass().getMethod
("freeStorageAndNotify", long.class, IPackageDataObserver.class);
method.invoke(mPM, Long.MAX_VALUE, new IPackageDataObserver.Stub() {
//子线程
@Override
public void onRemoveCompleted(String packageName, boolean succeeded)
throws RemoteException {
runOnUiThread(new Runnable() {
@Override
public void run() {
SaoMiaoCache();
}
});
} }); } catch (Exception e) {
e.printStackTrace();
}
}
class MyObserver extends IPackageStatsObserver.Stub { //在子线程运行
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws
RemoteException {
//缓存大小
cacheSize = pStats.cacheSize;
System.out.println(cacheSize);
runOnUiThread(new Runnable() {
@Override
public void run() {
btnCache.setText("缓存大小:" + Formatter.formatFileSize(getApplicationContext(), cacheSize));
}
});
}
} }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.loaderman.appcachedemo.MainActivity"> <Button
android:id="@+id/btn_cache"
android:onClick="startScan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="扫描缓存"/>
<Button
android:id="@+id/btn_clean"
android:onClick="cleanCache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清理缓存"/>
</LinearLayout>
添加权限:
<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE"/>
在main下面创建aidl文件
新建包名为:android.content.pm
IPackageDataObserver.aidl
/*
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/ package android.content.pm; /**
* API for package data change related callbacks from the Package Manager.
* Some usage scenarios include deletion of cache directory, generate
* statistics related to code, data, cache usage(TODO)
* {@hide}
*/
oneway interface IPackageDataObserver {
void onRemoveCompleted(in String packageName, boolean succeeded);
}
IPackageStatsObserver.aidl
/*
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/ package android.content.pm; import android.content.pm.PackageStats;
/**
* API for package data change related callbacks from the Package Manager.
* Some usage scenarios include deletion of cache directory, generate
* statistics related to code, data, cache usage(TODO)
* {@hide}
*/
oneway interface IPackageStatsObserver { void onGetStatsCompleted(in PackageStats pStats, boolean succeeded);
}
PackageStats.aidl
/* //device/java/android/android/view/WindowManager.aidl
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/ package android.content.pm;
parcelable PackageStats;
效果图:

跳到系统应用信息页面清理缓存的方法:
//跳到系统应用信息页面
Intent infoIntent = new Intent();
infoIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);//Uri.parse
// ("package:" + mCurrentInfo.packageName);
infoIntent.setData(uri);
startActivity(infoIntent);
手机APP缓存的获取和清理功能的实现的更多相关文章
- 手机App测试如何获取包名的入口【两种方式】
在进行手机APP测试的时候经常要获取包名.那么何为包名呢?简单来说其实就是手机APP的安装apk文件的名称,每个手机APP(软件)的包名都是唯一的. 那么我们怎样来获取包名以及包名的入口呢? 方法一: ...
- 利用WeX5给手机APP增加短信验证码功能
帖子来源:http://bbs.wex5.com/thread-70908-1-1.html 遇到一个手机APP项目客户要求注册到APP上的用户手机号必须是真实的通过X5平台整合短信发送平台接口完成了 ...
- Android开发之清除缓存功能实现方法,可以集成在自己的app中,增加一个新功能。
作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 Android开发之清除缓存功能实现方法,可以集成在自己的app中,增加一个新功能. 下面是一个效果图 ...
- 【Python】[技术博客] 一些使用Python编写获取手机App日志的操作
一些使用Python编写获取手机App日志的操作 如何获取手机当前打开的App的包名 如何获取当前App进程的PID 如何查看当前App的日志 如何将日志保存到文件 如何关闭进程 如何不显示命令行窗口 ...
- 手机app(功能)测试重点
在手机客户端进行查看的测试重点:1.“点击加载更多”的分页处理技术,是否有重复的数据,数据显示是否完整,到达最后一页后是否还有数据进行显示2.数据的排序方式2.界面跳转是否正确3.出现异常情况是否有提 ...
- 转:浅谈手机app测试注意点
现在我们测试时,开发会先在本地机上打好测试包,自己安装,轮完一轮,开发修改好后,再打一个包.以下是功能测试时需要注意的点: 1.登录 ●登录用户名和密码错误时,界面有提示信息 ●用户主动退出登录后,下 ...
- Python爬虫入门教程 41-100 Fiddler+夜神模拟器+雷电模拟器配置手机APP爬虫部分
爬前叨叨 从40篇博客开始,我将逐步讲解一下手机APP的爬虫,关于这部分,我们尽量简化博客内容,在这部分中可能涉及到一些逆向,破解的内容,这部分尽量跳过,毕竟它涉及的东西有点复杂,并且偏离了爬虫体系太 ...
- 银行手机APP安全评估报告【转载】
猫头鹰工作室 我不相信命运,但尊敬命运 主页 大数据 Kafka Spark Hbase Redis Flume ActiveMQ 渗透测试 方法论 Kali测试 APP安全 OWASP 脑图 Too ...
- 手机APP测试
注:以下内容来自网络: 一.手机APP测试类型 1.1 接口协议测试 在APP客户端开发设计时,一般服务端会提供相应的接口协议文档,接口协议文档的质量,决定了APP的开发进度.此部分的测试,应首先检测 ...
随机推荐
- shell脚本获取传递的参数
1 脚本编写 #!/bin/bash 2 解释 $n 表示是第几个参数 $0 表示脚本命令本身 3 执行效果
- contextlib:上下文管理器工具
介绍 contextlib模块包含的工具可以用于处理上下文管理器和with语句 上下文管理器API ''' 上下文管理器(context manager)负责管理一个代码块中的资源,会在进入代码块时创 ...
- python中的__init_subclass__是什么?
什么是__init_subclass__ class Hook: def __init_subclass__(cls, **kwargs): print("__init_subclass__ ...
- deep_learning_Function_Sklearn_Mode
API参考:https://scikit-learn.org/stable/modules/classes.html# 作为Python中经典的机器学习模块,sklearn围绕着机器学习提供了很多可直 ...
- Boost::pool (1)
POOL 什么是pool 池分配是一种非常快速的内存分配方案,但其使用受到限制.有关池分配的更多信息(也称为简单隔离存储,请参阅 池化概念和简单隔离存储). 我为什么要使用Pool? 使用池可以更好地 ...
- ELK展示NGINX访问IP地理位置图
一.设置NGINX日志格式 [root@zabbix_server ~]# vim /etc/nginx/nginx.conf log_format access_json_log '{"@ ...
- 第六章 组件 59 组件切换-使用Vue提供的component元素实现组件切换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- iOS 获取手机型号(已更新至iPhone11)
+ (NSString *)iphoneType { // 需要导入头文件:#import <sys/utsname.h> struct utsn ...
- Js基础知识(五) - 前端性能优化总结
前端性能优化总结 资源优化 缓存 最好的资源优化就是不加载资源.缓存也是最见效的优化手段.说实话,虽然说客户端缓存发生在浏览器端,但缓存主要还是服务端来控制,与我们前端关系并不是很大.但还是有必要了解 ...
- 使用IDEA 搭建一个 SpringBoot + Hibernate + Gradle 项目
现在创建个项目: 勾上 自已 需要东西.(这里作为演示) maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} 关闭项目 ...