this code is supposed to calculate total cache of all apps combined, but i am not able to test it. i want the calculated cache to be displayed in my textview, how do i do that?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myMethod();
}
private void myMethod() {
PackageManager packageManager = getPackageManager();
List<ApplicationInfo> packages = packageManager
.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
try {
Context mContext = createPackageContext(
packageInfo.packageName, CONTEXT_IGNORE_SECURITY);
File cacheDirectory = mContext.getCacheDir();
if (cacheDirectory == null) {
// cacheArrayList.add("0");
} else {
cacheArrayList
.add(String.valueOf(cacheDirectory.length() / 1024));
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
TextView myTextView = (TextView) findViewById(R.id.cacheSize);
myTextView.setText("YOUR STRING"); // replaces textview text
myTextView.append("YOUR STRING"); // appends to current textview's text
}
Textview is as below
<TextView
android:id="@+id/cacheSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="--" />