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. 第十二章·Kibana深入-日志图形展示

    1.Kibana创建区域图 Kibana支持多重图从展示功能,需要日志是json格式的支持. Kibana区域图 打开浏览器,访问:http://10.0.0.54:5601   选择一个日志  ...

  2. STM32WB SRAM2

    SRAM2存储: 1.挂接总线及地址大小 2.地址镜像 3.RDP(read protection)等级 4.不同等级下的访问状态 5.声明位于SRAM2区中的数据 1)在icf文件中定义region ...

  3. 建立一个能持续处理的C/S网络程序

    程序流程图: 代码演示: 服务器端: #include<WinSock2.h> #include<Windows.h> #include<stdio.h> #inc ...

  4. 使用Task的Wait和Result时注意

    如果计算限制的任务抛出未处理的异常,该异常会被“吞噬”并存储到一个集合中,而线程池线程可以返回到线程池中.调用Wait方法或者Result属性时,这些成员会抛出一个System.AggregateEx ...

  5. dsoframer控件注册,解注册和检查注册情况

      public class DsoframerHelper { private static string dsoframerPath = System.Windows.Forms.Applicat ...

  6. Springboot整合Ehcache 解决Mybatis二级缓存数据脏读 -详细

    前面有写了一篇关于这个,但是这几天又改进了一点,就单独一篇在详细说明一下 配置 application.properties ,启用Ehcache # Ehcache缓存 spring.cache.t ...

  7. linux内核 进程管理

    进程和线程 进程不单单包含可执行代码(代码段),好包含打开的文件,挂起的信号,处理器状态,虚拟内存地址等. 线程:从内核的角度来说,它并没有线程这个概念.Linux把所有线程都当做进程来实现.内核并没 ...

  8. jenkins 批量修改svn 地址

    svn服务器的ip变了,jenkins里那么多任务一个个修改要疯了, 每个已经创建后的任务的svn 地址都配置在 jenkins 主目录下的jobs 里对应的任务下的config.xml 里 在job ...

  9. stm32之HAL串口中断的callback流程图

  10. 自制centos6开机界面

    1.先准备好一张640x480大小的图片并上传至主机(可在画图工具中调整图片大小) 注意如没有rz命令,可以先安装: yum install lrzsz 2.制作背景图 制作需要用到convert命令 ...