Android应用程序使用java冻结

时间:2017-05-18 15:07:19

标签: java android multithreading memory memory-management

最近我开始学习java和android来构建一些Android应用程序。此外,我在编码方面有点新,第一个应用程序我的代码存在高数据和大量内存使用的问题。它的作用是:输入手机的确切地址,并显示目录中的所有文件和子文件。一切都运作良好,直到我给它一个包含大量文件的地址,一切都冻结了。任何反馈,建议和其他任何事情也会很高兴。这是代码:

MainActivity.java:

package com.mycompany.MagicFiles;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.*;
import java.util.*;
import java.text.*;

public class MainActivity extends Activity 
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

public void onShowButtonClick(View view)
{
    try
    {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        EditText editText = (EditText)findViewById(R.id.mainEditText1);
        TextView textView = (TextView) findViewById(R.id.mainTextView1);
        String dirName = editText.getText().toString();
        String temp = "";
        double bytes = 0;

        for (File tf : listf(dirName))
        {
            bytes = (tf.length() / 1024);
            temp = temp + tf.getAbsolutePath() + "\nDate: " + sdf.format(tf.lastModified()) + " Size: " + bytes + "KB\n";
        }
        textView.setText(temp);
    }
    catch (Exception e)
    {
        TextView textView = (TextView) findViewById(R.id.mainTextView1);
        textView.setText(e.toString());
    }
}

public static List<File> listf(String directoryName)
{
    File directory = new File(directoryName);

    List<File> resultList = new ArrayList<File>();

    // get all the files from a directory
    File[] fList = directory.listFiles();
    resultList.addAll(Arrays.asList(fList));
    for (File file : fList)
    {
        if (file.isFile())
        {
            System.out.println(file.getAbsolutePath());
        }
        else if (file.isDirectory())
        {
            resultList.addAll(listf(file.getAbsolutePath()));
        }
    }
    return resultList;
 } 
}

main.xml中:

 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">

<TextView
    android:layout_height="wrap_content"
    android:text="Enter the exact location"
    android:layout_width="wrap_content"
    android:layout_margin="10dp"
    android:layout_marginTop="15dp"/>

<TextView
    android:layout_height="wrap_content"
    android:text="And it shows the files"
    android:layout_width="wrap_content"
    android:layout_margin="10dp"/>

<EditText
    android:layout_height="wrap_content"
    android:ems="10"
    android:layout_width="match_parent"
    android:layout_margin="10dp"
    android:id="@+id/mainEditText1"/>

<Button
    android:layout_height="wrap_content"
    android:text="Show"
    android:layout_width="match_parent"
    android:layout_margin="10dp"
    android:id="@+id/mainButton1"
    android:onClick="onShowButtonClick"/>

<HorizontalScrollView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">

    <ScrollView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

        <TextView
            android:layout_height="wrap_content"
            android:text="TODO: this gonna show something up :D"
            android:layout_width="match_parent"
            android:id="@+id/mainTextView1"/>

    </ScrollView>

</HorizontalScrollView>

的AndroidManifest.xml:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.MagicFiles" >
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/the_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

如果需要其他任何东西(可能是apk ......),请告诉我。

1 个答案:

答案 0 :(得分:0)

                            public void onShowButtonClick(View view) {
                                new Thread() {
                                    @Override
                                    public void run() {
                                        final View progressBar = findViewById(R.id.progressBar);
                                        try {

                                            runOnUiThread(
                                                    new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            progressBar.setVisibility(View.VISIBLE);
                                                        }
                                                    });

                                            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
                                            String dirName = ((EditText) findViewById(R.id.mainEditText1)).getText().toString();
                                            String temp = "";
                                            double bytes = 0;

                                            for (File tf : listf(dirName)) {
                                                bytes = (tf.length() / 1024);
                                                temp = temp + tf.getAbsolutePath() + "\nDate: " + sdf.format(tf.lastModified()) + " Size: " + bytes + "KB\n";
                                            }

                                            final String finalTemp = temp;
                                            runOnUiThread(
                                                    new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            ((TextView) findViewById(R.id.mainTextView1)).setText(finalTemp);
                                                            progressBar.setVisibility(View.GONE);
                                                        }
                                                    });

                                        } catch (final Exception e) {
                                            runOnUiThread(
                                                    new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            progressBar.setVisibility(View.GONE);
                                                            ((TextView) findViewById(R.id.mainTextView1)).setText(e.toString());
                                                        }
                                                    });

                                        }

                                    }
                                }.start();

}

不要忘记在布局中添加进度条:

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="gone"/>