大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

AndroidAsyncTask用法巧用实例代码

Android AsyncTask

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、虚拟空间、营销软件、网站建设、卢氏网站维护、网站推广。

联系人导入

 new AsyncTask, Integer, ArrayList>() {
                    @Override
                    protected void onPreExecute() {
                      super.onPreExecute();
                      if (importingDialog != null)
                        importingDialog.show();
                    }

                    @Override
                    protected ArrayList doInBackground(ArrayList... params) {

                      int importCount = 0;
                      for (ContactInfo member : importListContact) {
                        if (member.isCheck()) {
                          importCount++;

                          contactInsertHelper.insertSysContacts2Xphone(member);
                          params[0].add(member);

                          publishProgress(importCount);
                        }
                      }
                      return params[0];
                    }

                    @Override
                    protected void onProgressUpdate(Integer... values) {
                      importingDialog.notifyByteProgress(count, values[0]);
                    }

                    @Override
                    protected void onPostExecute(ArrayList contactInfos) {
                      final ArrayList names = new ArrayList();
                      final ArrayList numbers = new ArrayList();
                      for (ContactInfo contactInfo : contactInfos) {
                        for (int i = 0; i < contactInfo.getPhones().size(); i++) {
                          numbers.add(contactInfo.getPhones().get(i).replaceAll(" ", ""));
                          names.add(contactInfo.getName());
                        }
                      }

                      Intent intent = new Intent("com.godinsec.importcomplete");
                      intent.putStringArrayListExtra("numbers", numbers);
                      intent.putStringArrayListExtra("names", names);
                      ImportContactActivity.this.sendBroadcast(intent);


                      Intent updateIntent = new Intent("com.godinsec.update.syscontacts");
                      ImportContactActivity.this.sendBroadcast(updateIntent);

                      handler.sendEmptyMessage(0);
                    }
                  }.execute(new ArrayList());

联系人删除

new LauncherContactDeleAsyncTask(mActivity, contactInfosDeles).execute(new ArrayList());
package com.godinsec.contacts.core;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import com.godinsec.contacts.R;
import com.godinsec.contacts.contactHelper.ContactInsertHelper;
import com.godinsec.contacts.db.ApplicationSQLManger;
import com.godinsec.contacts.dialog.ImportingDialog;
import com.godinsec.contacts.model.ContactInfo;
import java.util.ArrayList;
/**
 * Created by Safly on 2016/9/23.
 */
public class LauncherContactDeleAsyncTask extends AsyncTask, Integer, ArrayList> {
  private Context mContext = null;
  private ArrayList deleteListContacts;//总数
  private ContactInsertHelper contactInsertHelper;

  final ArrayList deleContactPhones = new ArrayList<>();

  private ImportingDialog importingDialog = null;
  int deleCount = 0;
  int deleAllCount = 0;

  public LauncherContactDeleAsyncTask(Context mContext, ArrayList contactInfos) {
    this.mContext = mContext;
    this.deleteListContacts = contactInfos;
    contactInsertHelper = new ContactInsertHelper(mContext);
  }


  @Override
  protected void onPreExecute() {
    super.onPreExecute();
    importingDialog = new ImportingDialog(mContext,mContext.getResources().getString(R.string.gsettings_dele_contacts));
    importingDialog.setCanceledOnTouchOutside(false);

    if (importingDialog != null){
      importingDialog.show();
    }

    for (ContactInfo member : deleteListContacts) {
      if (member.isCheck()) {
        deleAllCount++;
      }
    }
  }

  @Override
  protected ArrayList doInBackground(ArrayList... params) {
    for (ContactInfo member : deleteListContacts) {
      if (member.isCheck()) {
        //删除联系人
        long contactServerFlag = ApplicationSQLManger.getInstance().getContactServerFlagByLauncherContact(mContext, member);

        params[0].add(String.valueOf(contactServerFlag));
        deleCount += contactInsertHelper.delete(mContext, member);

        //传递要删除的号码
        for (String phone : member.getPhones()) {
          if (deleContactPhones != null) {
            deleContactPhones.add(phone);
          }
        }

        publishProgress(deleCount);
      }
    }
    return params[0];
  }

  @Override
  protected void onProgressUpdate(Integer... values) {
    importingDialog.notifyByteProgress(deleAllCount, values[0]);
  }

  @Override
  protected void onPostExecute(ArrayList contactIds) {
    super.onPostExecute(contactIds);

    if (importingDialog.isShowing()) {
      importingDialog.dismiss();
    }

    Intent updateIntent = new Intent("com.godinsec.update.syscontacts");
    mContext.sendBroadcast(updateIntent);

    Intent intent = new Intent("com.godinsec.delcontact");
    intent.putStringArrayListExtra("numbers", deleContactPhones);
    mContext.sendBroadcast(intent);
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


文章标题:AndroidAsyncTask用法巧用实例代码
当前网址:http://dzwzjz.com/article/jhhscp.html
在线咨询
服务热线
服务热线:028-86922220
TOP