如何申请异步任务到这个 [英] How to apply async task into this

查看:149
本文介绍了如何申请异步任务到这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试我仍然无法弄清楚如何将一个AsyncTask的入低于code经过几个小时。

我已经试过它的线程也不能工作。所有我想要做的是在后台运行扫描,并显示一个进度条。

  / **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
}私人无效populateView(){    清单< PackageInfo> adPackages = getAdPackages();
    软件包管理系统下午= getPackageManager();    清单<地图<字符串,字符串>>数据=新的ArrayList<地图<字符串,字符串>>(adPackages.size());
    对于(PackageInfo PKG:adPackages){
        地图<字符串,字符串> ATTRS =新的HashMap<字符串,字符串>();
        attrs.put(应用程序名,pm.getApplicationLabel(pkg.applicationInfo)的ToString());
        attrs.put(包名,pkg.packageName);
        data.add(ATTRS);
    }    的String [] =由新的String [] {
        应用程序名,
        包名
    };
    INT []为= INT新[] {
        android.R.id.text1,
        android.R.id.text2
    };
    SimpleAdapter适配器=新SimpleAdapter(
            这一点,数据,android.R.layout.two_line_list_item,从,到);    setListAdapter(适配器);
    mPackages = adPackages;
}

 私人列表< PackageInfo> getAdPackages(){
    SET< PackageInfo> adPackages =新的HashSet< PackageInfo>();// [...]
    清单< ApplicationInfo> appInfos = pm.getInstalledApplications(0);    对于(ApplicationInfo APPINFO:appInfos){        尝试{            // [重的东西]    返回新的ArrayList< PackageInfo>(adPackages);   }
}


解决方案

是可以做到这一点。

您有您的 getPackages 逻辑移至 doInBackground 的AsyncTask
而你要叫 publishProgress doInBackground 当你想更新进度条。

doInBackground 做了,那么 onPostExecute 被调用。将所有用于适配器和适配器本身在它的数据的逻辑。还设置了适配器的功能。

下面是你可以参考一些参考文档:

异步任务
<一href=\"http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html#WorkerThreads\"相对=nofollow>工作线程

下面是一些示例:

 私有类GetPackageTask扩展的AsyncTask&LT;无效,整数,列表&LT; PackageInfo&GT;&GT; {
 保护列表与LT; PackageInfo&GT; doInBackground(网址...网址){     //把getPackages您code在这里     //你可以调用发布就像是做了如下
     //的for(int i = 0; I&LT;计数;我++){
     //总计TOTALSIZE + = Downloader.downloadFile(网址[I]);
     // publishProgress((INT)((I /(浮点)数)* 100));
     //}     // adPackages是你从你的getPackages函数返回什么
     返回adPackages;
 } 保护无效onProgressUpdate(整数...进度){
     setProgressPercent(进展[0]);
 } 保护无效onPostExecute(列表&LT; PackageInfo&GT;的结果){
     //在这里,您将所有的setAdapter相关code
 }
}

的onCreate将包含

新DownloadFilesTask()执行();

After a few hours of trying I still cannot figure out how to incorporate an asynctask into the below code.

I have tried threading which didn't work either. All I want to do is to run the scan in the background and show a progressbar.

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

private void populateView() {

    List<PackageInfo> adPackages = getAdPackages();
    PackageManager pm = getPackageManager();

    List<Map<String, String>> data = new ArrayList<Map<String, String>>(adPackages.size());
    for(PackageInfo pkg : adPackages) {
        Map<String, String> attrs = new HashMap<String, String>();
        attrs.put("App Name", pm.getApplicationLabel(pkg.applicationInfo).toString());
        attrs.put("Package Name", pkg.packageName);
        data.add(attrs);
    }

    String[] from = new String[] {
        "App Name",
        "Package Name"
    };
    int[] to = new int[] {
        android.R.id.text1,
        android.R.id.text2
    };
    SimpleAdapter adapter = new SimpleAdapter(
            this, data, android.R.layout.two_line_list_item, from, to);

    setListAdapter(adapter);
    mPackages = adPackages;
}

.

private List<PackageInfo> getAdPackages() {
    Set<PackageInfo> adPackages = new HashSet<PackageInfo>();

//[...]
    List<ApplicationInfo> appInfos = pm.getInstalledApplications(0);

    for(ApplicationInfo appInfo : appInfos) {

        try {

            //[Heavy Stuff]

    return new ArrayList<PackageInfo>(adPackages);

   }
}

解决方案

Yes this can be done.

You have to move your getPackages logic to doInBackground of AsyncTask. And you have to call publishProgress from doInBackground when you want to update progress bar.

Once doInBackground is done, then onPostExecute is called. Put all the logic for data for adapter and adapter itself in it. Set the adapter also in the function.

Below are few reference docs you can refer:

Async Task Worker Threads

Here is some sample:

 private class GetPackageTask extends AsyncTask<Void, Integer, List<PackageInfo>> {
 protected List<PackageInfo> doInBackground(URL... urls) {

     // Put your code of getPackages in here

     // You can call publish like it is done below
     //for (int i = 0; i < count; i++) {
     //    totalSize += Downloader.downloadFile(urls[i]);
     //    publishProgress((int) ((i / (float) count) * 100));
     //}

     // adPackages is what you returning from your getPackages function
     return adPackages;
 }

 protected void onProgressUpdate(Integer... progress) {
     setProgressPercent(progress[0]);
 }

 protected void onPostExecute(List<PackageInfo> result) {
     // Here you will have all the setAdapter related code
 }
}

onCreate will contain

new DownloadFilesTask().execute();

这篇关于如何申请异步任务到这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆