-1

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="--" />
MaggotSauceYumYum
  • 402
  • 1
  • 5
  • 23

3 Answers3

1

after for-each loop make another to put data from cacheArrayList to some string

for(String s : cacheArrayList){
    string = string+s+"\n"
}

and then put this string to textView

Dmytro Chaban
  • 1,106
  • 1
  • 11
  • 19
0

If you are trying to set the text on a TextView you are able to setText or appendText.

TextView myTextView = (TextView)findViewById(R.id.cacheSize);
myTextView.setText("YOUR STRING"); // replaces textview text
myTextView.append("YOUR STRING"); // appends to current textview's text
MaggotSauceYumYum
  • 402
  • 1
  • 5
  • 23
Seaniqua
  • 53
  • 8
0

create a global variable

TextView txt_view;

inside oncreate method of the activity add,

txt_view = findViewById(R.id.cacheSize);

add this method in your activity

public void displaycache(){
                int k=0;
//on assuming cacheArrayList is a global variable
                for(int i =0;i<cacheArrayList.size();i++){
                   k+=Integer.parseInt(cacheArrayList.get(i));
                 }
                txt_view.setText(String.valueOf(k));
            }

call displaycache(); after you call mymethod();

replace 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();
            File[] files = cacheDirectory.listFiles();
            for (File f:files) {
                cacheArrayList.add(String.valueOf(f.length() / 1024));
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}
Deepak John
  • 967
  • 1
  • 7
  • 19
  • i got this incomplete code from some answer, what do i do about cacheArrayList? – MaggotSauceYumYum Jan 05 '16 at 05:59
  • here is the question and i am trying to calculate total cache and show it in my textview. http://stackoverflow.com/questions/16206722/how-to-get-the-exact-size-of-cache-directory-android – MaggotSauceYumYum Jan 05 '16 at 06:01
  • create global variable ArrayList cacheArrayList = new ArrayList(); – Deepak John Jan 05 '16 at 06:04
  • it is not working, it always shows the same value in my textview even if i clear cache , it shows "496" – MaggotSauceYumYum Jan 05 '16 at 06:11
  • in http://stackoverflow.com/questions/16206722/how-to-get-the-exact-size-of-cache-directory-android there is anwer selected as correct use that code to get the size, your current code calculates size of cache directory's name not size occupied by files in the directory – Deepak John Jan 05 '16 at 06:13
  • could you please tell me how do i put that snippet in my answer so that it calculates the correct cache size? thanks for the help! – MaggotSauceYumYum Jan 05 '16 at 06:17