Android AsyncTask:跳过X帧 - 主线程内部工作量太大

时间:2013-07-09 22:27:43

标签: android android-fragments android-asynctask

我正在为一些网站制作一些RSS阅读器,所以我想在4.0版本的Androd的较低版本上实现actionbar和viewpager,所以我使用的是Jake Wharton的ActionBarSherlock和ViewPagerIndicator,所以我正在使用片段。

我想从URL读取一些RSS提要,我有一个名为LoadXMLData的AsyncTask类,这里是该类的代码。

LoadXMLData 类:

public class LoadXMLData extends AsyncTask<String, RSSFeed, RSSFeed>{


private ProgressDialog mProgressDialog;


private Context context;
RSSFeed feed;
private String RSSFEEDURL = "http://balkanandroid.com/feed/";

public LoadXMLData(Context context) {
    this.context = context;
    mProgressDialog = new ProgressDialog(context);
    mProgressDialog.setMessage("Molimo Vas, sačekajte. Podaci se učitavaju.");
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    mProgressDialog.show();
    Log.d("OVDE SAM:", "onPreExecute()");
}

@Override
protected RSSFeed doInBackground(String... urls) {
    // Obtain feed
    DOMParser myParser = new DOMParser();
    feed = myParser.parseXml(urls[0]);
    Log.d("OVDE SAM:", "PARSIRAM XML");
    return feed;
}



@Override
protected void onPostExecute(RSSFeed result) {
    mProgressDialog.dismiss();
    super.onPostExecute(result);
}

}

我还有 MainActivity 类,它扩展了SherlockFragmentActivity。

public class MainActivity extends SherlockFragmentActivity {
BAFragmentAdapter mAdapter;
RSSFeed feed;
Application myApp;

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

    Log.d("OVDE SAM:", "MAIN ACTIVITY");

    myApp = getApplication();



    mAdapter = new BAFragmentAdapter(getSupportFragmentManager());

    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(mAdapter);

    TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(pager);

}

public RSSFeed getFeed() {
    return feed;
}

public void setFeed(RSSFeed feed) {
    this.feed = feed;
}

最重要的是我有几个片段类(LatestFragments,PhonesFragments,TabletFragments,ApplicationFragments等),并且在每个片段上我的代码几乎相同,如下所示。

以下是 LatestFragment 的完整代码:

public class LatestFragment extends Fragment {

GridView lv;
RSSFeed feed;
CustomListAdapter adapter;
private String RSSFEEDURL = "http://balkanandroid.com/feed/";

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_najnovije, container,
            false);


    AsyncTask<String, RSSFeed, RSSFeed> xml = new LoadXMLData(getActivity())
            .execute(RSSFEEDURL);

    // AsyncTask<String, RSSFeed, RSSFeed> load = new
    // LoadXMLData(getActivity()).execute(RSSFEEDURL);

    try {
        feed = xml.get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    lv = (GridView) view.findViewById(R.id.GridView1);

    // Set an Adapter to the ListView
    adapter = new CustomListAdapter();
    lv.setAdapter(adapter);

    // Set on item click listener to the ListView
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // actions to be performed when a list item clicked
            int pos = arg2;

            Bundle bundle = new Bundle();
            bundle.putSerializable("feed", feed);
            Intent intent = new Intent(getActivity(), DetailsActivity.class);
            intent.putExtras(bundle);
            intent.putExtra("pos", pos);
            startActivity(intent);

        }
    });

    return view;
}

@Override
public void onDestroy() {
    super.onDestroy();
    adapter.imageLoader.clearCache();
    adapter.notifyDataSetChanged();
}

class CustomListAdapter extends BaseAdapter {

    private LayoutInflater layoutInflater;
    public ImageLoader imageLoader;

    public CustomListAdapter() {

        layoutInflater = (LayoutInflater) getActivity().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        imageLoader = new ImageLoader(getActivity().getApplicationContext());
    }

    public int getCount() {
        // TODO Auto-generated method stub
        // Set the total list item count
        return feed.getItemCount();
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // Inflate the item layout and set the views
        View listItem = convertView;
        int pos = position;
        if (listItem == null) {
            listItem = layoutInflater.inflate(R.layout.list_item, null);
        }

        // Initialize the views in the layout
        ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
        TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
        TextView tvDate = (TextView) listItem.findViewById(R.id.tvDate);

        // Set the views in the layout
        imageLoader.DisplayImage(feed.getItem(pos).getImage(), iv);
        tvTitle.setText(feed.getItem(pos).getTitle());
        tvDate.setText(feed.getItem(pos).getDate());

        return listItem;
    }

}

}

如您所见,我使用 AsyncTask类LoadXMLData 来读取 doInBackground()方法中的数据,然后使用此代码在所有片段类下调用该AsyncTask类,因为我需要RSSFeed的结果,因为我需要将这些数据显示给用户。

  AsyncTask<String, RSSFeed, RSSFeed> xml = new LoadXMLData(getActivity())
            .execute(RSSFEEDURL);   

    try {
        feed = xml.get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

它可以工作,但是当我点击从视图寻呼机打开其他视图时它很慢,几乎冻结了我的应用程序,我收到了Logcat的消息。

07-10 00:22:40.598:I / Choreographer(623):跳过316帧!应用程序可能在其主线程上做了太多工作。

1 个答案:

答案 0 :(得分:5)

这是因为您在get()上阻止了AsyncTask。不要这样做。

相反,请使用onPostExecute()的{​​{1}}中的Feed。