-1

我正在解析存储在我的设备 SD 卡中的 XML 文件。启动活动大约需要以上时间。

这就是我在我的日志中得到的

09-23 17:30:15.083: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 57ms
09-23 17:30:15.938: D/dalvikvm(27040): GC_CONCURRENT freed 1498K, 11% free 15192K/16887K, paused 12ms+13ms, total 122ms
09-23 17:30:15.939: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 98ms
09-23 17:30:16.772: D/dalvikvm(27040): GC_CONCURRENT freed 1939K, 13% free 15195K/17335K, paused 12ms+23ms, total 256ms
09-23 17:30:16.774: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 235ms
09-23 17:30:17.252: D/dalvikvm(27040): GC_CONCURRENT freed 1910K, 13% free 15204K/17335K, paused 1ms+3ms, total 161ms
09-23 17:30:17.253: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 98ms
09-23 17:30:17.955: D/dalvikvm(27040): GC_CONCURRENT freed 1912K, 13% free 15212K/17335K, paused 14ms+12ms, total 139ms
09-23 17:30:17.955: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 93ms
09-23 17:30:18.720: D/dalvikvm(27040): GC_CONCURRENT freed 1914K, 13% free 15217K/17335K, paused 13ms+3ms, total 153ms
09-23 17:30:18.722: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 86ms
09-23 17:30:19.349: D/dalvikvm(27040): GC_CONCURRENT freed 1912K, 13% free 15224K/17335K, paused 2ms+2ms, total 140ms
09-23 17:30:19.351: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 85ms
09-23 17:30:20.094: D/dalvikvm(27040): GC_CONCURRENT freed 1906K, 13% free 15232K/17335K, paused 2ms+13ms, total 169ms
09-23 17:30:20.096: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 157ms
09-23 17:30:21.848: D/dalvikvm(27040): GC_CONCURRENT freed 1899K, 13% free 15237K/17335K, paused 14ms+21ms, total 443ms
09-23 17:30:21.849: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 310ms
09-23 17:30:22.233: D/dalvikvm(27040): GC_CONCURRENT freed 1957K, 13% free 15245K/17399K, paused 5ms+2ms, total 96ms
09-23 17:30:22.234: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 70ms
09-23 17:30:22.589: D/dalvikvm(27040): GC_CONCURRENT freed 1912K, 13% free 15252K/17399K, paused 1ms+3ms, total 68ms
09-23 17:30:22.590: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 53ms
09-23 17:30:23.267: D/dalvikvm(27040): GC_CONCURRENT freed 1914K, 13% free 15258K/17399K, paused 3ms+198ms, total 454ms
09-23 17:30:23.268: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 435ms
09-23 17:30:23.639: D/dalvikvm(27040): GC_CONCURRENT freed 1912K, 13% free 15265K/17399K, paused 14ms+2ms, total 88ms
09-23 17:30:23.641: D/dalvikvm(27040): WAIT_FOR_CONCURRENT_GC blocked 49ms

代码活动

resultTrips = (LinearLayout) findViewById(R.id.trip_list);
LinearLayout.LayoutParams mainTripDetailsLayout = new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = (LayoutInflater) getBaseContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout.LayoutParams forUnderLine = new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
forUnderLine.setMargins(0, 0, 0, 0);
mainTripDetailsLayout.setMargins(0, 40, 0, 0);
File fXmlFile = new File("/storage/sdcard1/Android/myXML.xml");
if(fXmlFile.exists()){
    obj = new ParseMyXML();
    MyTripRespone data = obj.parseMyTripXML(fXmlFile);
    List<Approvals> listdata = data.getCoroprateBookingDetails();
    int i=0;
    for (Approvals elements : listdata) {

        TextView line = new TextView(this);
        line.setBackgroundResource(R.layout.shape_line);
        line.setLayoutParams(forUnderLine);
        if (i != 0) {
            resultTrips.addView(line);
        }

        final LinearLayout tripdetailsdata = (LinearLayout) inflater.inflate(
                R.layout.tripdetailsdata, null);
        try {
            tripDate = input.parse(elements.getJourneyDate());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        tripdetailsdata.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
            Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)", Toast.LENGTH_LONG).show();    
            }
        });
        i++;
    }
}
TextView dummy = new TextView(this);
dummy.setLayoutParams(mainTripDetailsLayout);
resultTrips.addView(dummy);

请建议我必须进行哪些更改才能使我的应用程序变得更快。它非常慢。请建议我是 android 新手

4

2 回答 2

1

在我看来,主要的事情不是在 GUI 线程中实现解析器。最简单的方法是使用AsyncTask的扩展来执行 XML 并在 GUI 线程中显示结果。

于 2013-09-23T12:18:05.533 回答
0

更新答案试试这个

public class ParseXMLTask extends AsyncTask<String, Void, MyTripRespone> {
    ParseMyTripResponseXML obj;
    Context context;
    CallBack callBack;

    public ParseXMLTask(CallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected MyTripRespone doInBackground(String... arg0) {
        obj = new ParseMyTripResponseXML();
        MyTripRespone data = obj.parseMyTripXML(SelfApprovals.fXmlFile);
        return data;
    }
    protected void onPostExecute(MyTripRespone result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        callBack.run(result);

    }
}
于 2013-09-23T12:22:55.910 回答