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缓存的获取和清理功能的实现的更多相关文章

  1. 手机App测试如何获取包名的入口【两种方式】

    在进行手机APP测试的时候经常要获取包名.那么何为包名呢?简单来说其实就是手机APP的安装apk文件的名称,每个手机APP(软件)的包名都是唯一的. 那么我们怎样来获取包名以及包名的入口呢? 方法一: ...

  2. 利用WeX5给手机APP增加短信验证码功能

    帖子来源:http://bbs.wex5.com/thread-70908-1-1.html 遇到一个手机APP项目客户要求注册到APP上的用户手机号必须是真实的通过X5平台整合短信发送平台接口完成了 ...

  3. Android开发之清除缓存功能实现方法,可以集成在自己的app中,增加一个新功能。

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 Android开发之清除缓存功能实现方法,可以集成在自己的app中,增加一个新功能. 下面是一个效果图 ...

  4. 【Python】[技术博客] 一些使用Python编写获取手机App日志的操作

    一些使用Python编写获取手机App日志的操作 如何获取手机当前打开的App的包名 如何获取当前App进程的PID 如何查看当前App的日志 如何将日志保存到文件 如何关闭进程 如何不显示命令行窗口 ...

  5. 手机app(功能)测试重点

    在手机客户端进行查看的测试重点:1.“点击加载更多”的分页处理技术,是否有重复的数据,数据显示是否完整,到达最后一页后是否还有数据进行显示2.数据的排序方式2.界面跳转是否正确3.出现异常情况是否有提 ...

  6. 转:浅谈手机app测试注意点

    现在我们测试时,开发会先在本地机上打好测试包,自己安装,轮完一轮,开发修改好后,再打一个包.以下是功能测试时需要注意的点: 1.登录 ●登录用户名和密码错误时,界面有提示信息 ●用户主动退出登录后,下 ...

  7. Python爬虫入门教程 41-100 Fiddler+夜神模拟器+雷电模拟器配置手机APP爬虫部分

    爬前叨叨 从40篇博客开始,我将逐步讲解一下手机APP的爬虫,关于这部分,我们尽量简化博客内容,在这部分中可能涉及到一些逆向,破解的内容,这部分尽量跳过,毕竟它涉及的东西有点复杂,并且偏离了爬虫体系太 ...

  8. 银行手机APP安全评估报告【转载】

    猫头鹰工作室 我不相信命运,但尊敬命运 主页 大数据 Kafka Spark Hbase Redis Flume ActiveMQ 渗透测试 方法论 Kali测试 APP安全 OWASP 脑图 Too ...

  9. 手机APP测试

    注:以下内容来自网络: 一.手机APP测试类型 1.1 接口协议测试 在APP客户端开发设计时,一般服务端会提供相应的接口协议文档,接口协议文档的质量,决定了APP的开发进度.此部分的测试,应首先检测 ...

随机推荐

  1. c++11 移动语义move semantics

    performance, expensive object copies move semantics, temporary objects implemented with rvalue refer ...

  2. jsp页面获取浏览器中的请求 或者 转发过来的请求值

    jsp页面中的$(param.xxx)   $(param.user)相当于<%=request.getParameter("user")%>

  3. java-接口—策略模式

    策略模式,就是不同类继承相同的接口,实现不同的策略.

  4. 数据库——Oracle(1)

    1 Oracle数据库:ORACLE数据库系统是美国ORACLE(甲骨文)研发并提供的款关系型数据库管理系统,占据市场的主要的份额. 目前常用版本:Oracle9i,Oracle10g,Oracle1 ...

  5. zencart批量评论插件Easy Populate CSV add reviews使用教程

    此插件在Easy Populate CSV 1.2.5.7b产品批量插件基础上开发,有1.3x与1.5x两个版本. zencart批量评论插件Easy Populate CSV add reviews ...

  6. dedecms织梦后台发布文章提示“标题不能为空”的解决办法

    V5.7登录后台后,发布英文标题没问题,发布中文会提示“标题不能为空”. 原因:htmlspecialchars在php5.4默认为utf8编码,gbk编码字符串经 htmlspecialchars ...

  7. android 拍照上传文件 原生定位

    最近公司需要一个android拍照上传和定位功能的的单一功能页面,一开始选择ionic cordova angular的一套H5框架,但是遇到和上传文件报错的问题,bug找了一天没找到原因,怀疑是io ...

  8. Springboot + Mybatis + Ehcache

    最近在做一个项目,为处理并发性较差的问题,使用了Mybatis二级缓存 但在多表联合查询的情况下,Mybatis二级缓存是存在着数据脏读的问题的 两天就是在想办法解决这个数据脏读的问题 考虑到简易性. ...

  9. 【杂题】cf1041fF. Ray in the tube

    死于没有处理边界 题目描述 题目大意 在两面镜子上各选定一个整数位置的点 A 与 B,并从其中一个点向另一个射出一条光线,使得接收到光线的传感器数量尽可能的多.传感器不重叠. 题目分析 我们来初步考虑 ...

  10. Vue数据通信详解

    如果有需要源代码,请猛戳源代码 希望文章给大家些许帮助和启发,麻烦大家在GitHub上面点个赞!!!十分感谢 一.前言 组件是 vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着 ...