安卓手机 扫描sd卡目录,查找所有cache 文件,进行删除,用过之后 可清除掉 很多原来存留的文件,节省sd 卡空间,
删除文件 比较暴力,有重要文件请自行保存~~~
activity code
import android.Manifest;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.StatFs;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.io.File;
import java.util.Calendar;
import java.util.logging.SimpleFormatter;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_EXTERNAL_STRONGE = 101;
private static final int ACCESS_NETWORK_STATE = 102;
private static final String TAG = "lsw";
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
private String netInfo;
private TextView mNetInfo_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
// tv.setText(stringFromJNI());
mNetInfo_tv = findViewById(R.id.netInfo_tv);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startDeleteFile();
}
});
mNetInfo_tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkNet();
}
});
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STRONGE);
} else {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
double blockSize = stat.getBlockSize();
double availableBlocks = stat.getAvailableBlocks();
double blockCount = stat.getBlockCount();
// LogUtil.e(" 可以用的空间 --- " + (availableBlocks * blockSize) / (1024 * 1024 * 1000) + "G");
TextView canUseCount_tv = findViewById(R.id.canUseCount_tv);
TextView count_tv = findViewById(R.id.count_tv);
TextView memInfo_tv = findViewById(R.id.memInfo_tv);
String canUser = (availableBlocks * blockSize) / (1024 * 1024 * 1024) + "";
canUseCount_tv.setText(" 可以用的空间 --- " + canUser.substring(0, 5) + "G");
String total = (blockCount * blockSize) / (1024 * 1024 * 1024) + "";
count_tv.setText(" 总可用的空间 --- " + total.substring(0, 5) + "G");
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
am.getMemoryInfo(outInfo);
long availMem = outInfo.availMem;
long totalMem = outInfo.totalMem;
memInfo_tv.setText("总内存 = " + Formatter.formatFileSize(this, totalMem) + " 可用内存 = " + Formatter.formatFileSize(this, availMem));
// Log.e(TAG, "availMem = " + Formatter.formatFileSize(this, availMem));
// Log.e(TAG, "totalMem = " + Formatter.formatFileSize(this, totalMem));
}
}
/**
* delete file by modify date to the file;
*/
private void startDeleteFile() {
//Scan folder
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
File[] files = folder.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
// 隐藏文件 ,和系统文件不能删除
if (f.isDirectory() && !f.getName().contains("Android") || !f.getName().startsWith(".")) {
deleteFile(f);
}
}
}
private void deleteFile(File file) {
//flie:要删除的文件夹的所在位置
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
File f = files[i];
deleteFile(f);
}
// file.delete();//如要保留文件夹,只删除文件,请注释这行
} else if (file.exists()) {
//
Calendar cal = Calendar.getInstance();
long time = file.lastModified();
cal.setTimeInMillis(time);
Calendar deleteCanlendar = Calendar.getInstance();
deleteCanlendar.set(Calendar.YEAR, 2018);
deleteCanlendar.set(Calendar.MONTH, 1);
deleteCanlendar.set(Calendar.DAY_OF_MONTH, 1);
// LogUtil.e(time+ " -- time"+ cal.getTime().toLocaleString());
// LogUtil.e(deleteCanlendar.getTimeInMillis()+ " -- "+deleteCanlendar.getTime().toLocaleString());
// if( time < deleteCanlendar.getTimeInMillis() ){
if (file.getAbsolutePath().contains("Cache") || file.getAbsolutePath().contains("cache"))
//此处toLocalString()方法是不推荐的,但是仍可输出
LogUtil.e("修改时间 " + cal.getTime().toLocaleString() + " --- " + file.getAbsolutePath());
file.delete();
// }else{
//
// }
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//根据请求是否通过的返回码进行判断,然后进一步运行程序
if (grantResults.length > 0 && requestCode == REQUEST_EXTERNAL_STRONGE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
LogUtil.e("you REQUEST_EXTERNAL_STRONGE ");
} else {
}
if (grantResults.length > 0 && requestCode == ACCESS_NETWORK_STATE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
LogUtil.e("you ACCESS_NETWORK_STATE ");
} else {
}
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
public native Boolean deleteFileNative(File f);
public String getNetState() {
//获得网络连接服务
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo.State state = connManager.getActiveNetworkInfo().getState();
if (state != NetworkInfo.State.CONNECTED) {
netInfo = "当前没有网络!";
}
state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState(); // 获取网络连接状态
if (NetworkInfo.State.CONNECTED == state) { // 判断是否正在使用WIFI网络
netInfo = "WIFI";
}
state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); // 获取网络连接状态
if (NetworkInfo.State.CONNECTED != state) { // 判断是否正在使用GPRS网络
netInfo = "TYPE_MOBILE";
}
return netInfo;
}
/**
* 获取当前的网络状态 :没有网络-0:WIFI网络1:4G网络-4:3G网络-3:2G网络-2
* 自定义
*
* @param context
* @return
*/
public static int getNETType(Context context) {
//结果返回值
int netType = 0;
//获取手机所有连接管理对象
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取NetworkInfo对象
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
//NetworkInfo对象为空 则代表没有网络
if (networkInfo == null) {
return netType;
}
//否则 NetworkInfo对象不为空 则获取该networkInfo的类型
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_WIFI) {
//WIFI
netType = 1;
} else if (nType == ConnectivityManager.TYPE_MOBILE) {
int nSubType = networkInfo.getSubtype();
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//3G 联通的3G为UMTS或HSDPA 电信的3G为EVDO
if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
&& !telephonyManager.isNetworkRoaming()) {
netType = 4;
} else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
|| nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
|| nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
&& !telephonyManager.isNetworkRoaming()) {
netType = 3;
//2G 移动和联通的2G为GPRS或EGDE,电信的2G为CDMA
} else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
|| nSubType == TelephonyManager.NETWORK_TYPE_EDGE
|| nSubType == TelephonyManager.NETWORK_TYPE_CDMA
&& !telephonyManager.isNetworkRoaming()) {
netType = 2;
} else {
netType = 2;
}
}
return netType;
}
private void checkNet() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.ACCESS_WIFI_STATE}, ACCESS_NETWORK_STATE);
} else {
// 没有网络-0:WIFI网络1:4G网络-4:3G网络-3:2G网络-2
switch (getNETType(this)) {
case 0:
mNetInfo_tv.setText("没有网络");
break;
case 1:
mNetInfo_tv.setText("WIFI网络");
break;
case 2:
mNetInfo_tv.setText("2G网络");
break;
case 3:
mNetInfo_tv.setText("3G网络");
break;
case 4:
mNetInfo_tv.setText("4G网络");
break;
}
}
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<TextView
android:id="@+id/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始清理缓存文件!"
android:layout_marginTop="10dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="20sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/canUseCount_tv"
android:hint=""
android:textSize="20sp"
android:maxLength="18"
app:layout_constraintTop_toBottomOf="@+id/sample_text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/count_tv"
android:hint=""
android:textSize="20sp"
android:maxLength="18"
app:layout_constraintTop_toBottomOf="@+id/canUseCount_tv"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="@+id/memInfo_tv"
android:hint=""
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@+id/count_tv"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="@+id/netInfo_tv"
android:hint="检测网络"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@+id/memInfo_tv"
/>
<EditText
android:id="@+id/year_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入年"
app:layout_constraintTop_toBottomOf="@+id/sample_text"
android:visibility="gone"
/>
<EditText
android:id="@+id/mouth_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入月"
app:layout_constraintTop_toBottomOf="@+id/year_et"
android:visibility="gone"
/>
<EditText
android:id="@+id/day_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入日"
app:layout_constraintTop_toBottomOf="@+id/mouth_et"
android:visibility="gone"
/>
</android.support.constraint.ConstraintLayout>