欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android AsyncTask用法巧用实例代码

程序员文章站 2024-02-12 19:04:52
android asynctask 联系人导入 new asynctask, integer,...

android asynctask

联系人导入

 new asynctask<arraylist<contactinfo>, integer, arraylist<contactinfo>>() {
                    @override
                    protected void onpreexecute() {
                      super.onpreexecute();
                      if (importingdialog != null)
                        importingdialog.show();
                    }

                    @override
                    protected arraylist<contactinfo> doinbackground(arraylist<contactinfo>... 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<contactinfo> contactinfos) {
                      final arraylist<string> names = new arraylist<string>();
                      final arraylist<string> numbers = new arraylist<string>();
                      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<contactinfo>());

联系人删除

new launchercontactdeleasynctask(mactivity, contactinfosdeles).execute(new arraylist<string>());

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<arraylist<string>, integer, arraylist<string>> {
  private context mcontext = null;
  private arraylist<contactinfo> deletelistcontacts;//总数
  private contactinserthelper contactinserthelper;

  final arraylist<string> delecontactphones = new arraylist<>();

  private importingdialog importingdialog = null;
  int delecount = 0;
  int deleallcount = 0;

  public launchercontactdeleasynctask(context mcontext, arraylist<contactinfo> 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<string> doinbackground(arraylist<string>... 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<string> 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);
  }
}

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