IntentService 上传多图

如果没兴趣的可以直接看实例。
首先把 原文 搬过来:IntentService是Service类的子类,用来处理异步请求。客户端可以通过startService(Intent)方法传递请求给IntentService,IntentService通过worker thread处理每个Intent对象,执行完一个Intent请求对象所对应的工作之后,如果没有新的Intent请求达到,则自动停止Service;否则执行下一个Intent请求所对应的任务,执行完所有的工作之后自动停止Service。
IntentService在处理事务时,还是采用的Handler方式,创建一个名叫ServiceHandler的内部Handler,并把它直接绑定到HandlerThread所对应的子线程。 ServiceHandler把处理一个intent所对应的事务都封装到叫做onHandleIntent的虚函数;因此我们直接实现虚函数onHandleIntent,再在里面根据Intent的不同进行不同的事务处理就可以了。
  说明:worker thread处理所有通过传递过来的请求,创建一个worker queue,一次只传递一个intent到onHandleIntent中,从而不必担心多线程带来的问题。处理完毕之后自动调用stopSelf()方法;默认实现了Onbind()方法,返回值为null;
  模式实现了onStartCommand()方法,这个方法会放到worker queue中,然后在onHandleIntent()中执行0。
  使用IntentService需要两个步骤:
  1、写构造函数
  2、复写onHandleIntent()方法(根据Intent的不同进行不同的事务处理)
注意:IntentService的构造函数一定是参数为空的构造函数,然后再在其中调用super("classname")这种形式的构造函数。
因为Service的实例化是系统来完成的,而且系统是用参数为空的构造函数来实例化Service的
---------------------以上是一些介绍和个人------------------------
写一个Demo来模拟两个耗时操作 Operation1与Operation2,

执行完一个Intent请求对象所对应的工作之后,如果没有新的Intent请求达到,则自动停止Service;否则执行下一个Intent请求所对应的任务,执行完所有的工作之后自 动停止Service。

看是不是先执行1,2必须等1执行完才能执行:

public class UploadImgService extends IntentService {  

     public UploadImgService() {  
         //必须实现父类的构造方法  
         super("UploadImgService");  
     }  
  
     @Override  
     public IBinder onBind(Intent intent) {  
         System.out.println("onBind");  
         return super.onBind(intent);  
     }  

     @Override  
     public void onCreate() {  
         System.out.println("onCreate");  
         super.onCreate();  
     }  

     @Override  
     public void onStart(Intent intent, int startId) {  
         System.out.println("onStart");  
         super.onStart(intent, startId);  
     }  


     @Override  
     public int onStartCommand(Intent intent, int flags, int startId) {  
         System.out.println("onStartCommand");  
         return super.onStartCommand(intent, flags, startId);  
     }  


     @Override  
     public void setIntentRedelivery(boolean enabled) {  
         super.setIntentRedelivery(enabled);  
         System.out.println("setIntentRedelivery");  
     }  

     @Override  
     protected void onHandleIntent(Intent intent) {  
         //Intent是从Activity发过来的,携带识别参数,根据参数不同执行不同的任务  
         String action = intent.getExtras().getString("param");  
         if (action.equals("oper1")) {  
             System.out.println("Operation1");  
         }else if (action.equals("oper2")) {  
             System.out.println("Operation2");  
         }    
         try {  
             Thread.sleep(2000);  
         } catch (InterruptedException e) {  
             e.printStackTrace();  
         }  
     }  

     @Override  
     public void onDestroy() {  
         System.out.println("onDestroy");  
         super.onDestroy();  
     }  
 }

把生命周期方法全打印出来了,待会我们来看看它执行的过程是怎样的。接下来是Activity,在Activity中来启动IntentService:

public class TestActivity extends Activity {  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);     
        //可以启动多次,每启动一次,就会新建一个work thread,但IntentService的实例始终只有一个  
        //Operation 1  
        Intent startServiceIntent = new Intent(this,UploadImgService.class);
        Bundle bundle = new Bundle();  
        bundle.putString("param", "oper1");  
        startServiceIntent.putExtras(bundle);  
        startService(startServiceIntent);  
      
        //Operation 2  
        Intent startServiceIntent2 = new Intent(this,UploadImgService.class);
        Bundle bundle2 = new Bundle();  
        bundle2.putString("param", "oper2");  
        startServiceIntent2.putExtras(bundle2);  
        startService(startServiceIntent2);  
    }  
}

别急着运行看效果,还有配置Service,因为它继承于Service,所以,它还是一个Service,一定要配置,否则是不起作用的

<service android:name="com.naplec.service.UploadImgService"></service>

自己看下打印的就应该差不多明白了

以上呢是IntentService用法,现在我们进入正题,用IntentService上传图片。直接贴代码,不明白可以问

public class UploadImgService extends IntentService {
    private static final String ACTION_UPLOAD_IMG = "com.naplec.action.UPLOAD_IMAGE";
    public static final String EXTRA_IMG_PATH = "com.naplec.service.IMG_PATH";
    public static final String EXTRA_IMG_PARAMS = "com.naplec.service.IMG_PARAMS";
    public static final String BACK_RESULT = "com.naplec.service.BACK_RESULT";

    public static void startUploadImg(Context context, ArrayList<String> isList, HashMap<String, String> data) {
        Intent intent = new Intent(context, UploadImgService.class);
        intent.setAction(ACTION_UPLOAD_IMG);
        intent.putExtra(EXTRA_IMG_PATH, isList);
        intent.putExtra(EXTRA_IMG_PARAMS, data);
        context.startService(intent);
    }


    public UploadImgService() {
        super("UploadImgService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_UPLOAD_IMG.equals(action)) {
                List<String> isList = (List<String>) intent.getSerializableExtra(EXTRA_IMG_PATH);
                Map<String, String> data = (Map<String, String>)     intent.getSerializableExtra(EXTRA_IMG_PARAMS);
                handleUploadImg(isList, data);
            }
        }
    }

    private void handleUploadImg(List<String> isList, Map<String, String> data) {
        try {
            String upFileUrl = "替换为你自己的地址";
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(upFileUrl);
            // 要把数据封装到post
            /**
             *  
             *  httpmime-v4.2.3
             *  httpmime-4.3.5.jar 依赖  httpcore-4.3.2 -- MultiPartEntity.addPart()
             *  以上两个是不同的
             *  我用的是 httpmime-v4.2.3 ,本文最后有下载地址
             */
            MultipartEntity entity = new MultipartEntity();
            for (Map.Entry<String, String> entry : data.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                entity.addPart(key, new StringBody(value, Charset.forName("UTF-8")));
            }
            int isListSize = isList.size();
            if (isListSize > 0) {
                for (int i = 0, len = isListSize; i < len; i++) {
                    // 循环获取每个image的路径
                    String sourcePath = isList.get(i).getSourcePath();
                    // 将每一个图片都转成二进制流文件
                    InputStream is = new FileInputStream(new File(sourcePath));
                    String filename = getImgName(sourcePath);
                    // 二进制的流文件数据封装
                    entity.addPart("upfile", new InputStreamBody(is, "multipart/form-data", filename));
                }
            }

            post.setEntity(entity);

            HttpResponse response = client.execute(post);

            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                System.out.println("请求服务器出错");
            }
            String result = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);

            Intent intent = new Intent(TestActivity.UPLOAD_RESULT);
            intent.putExtra(BACK_RESULT, result);
            sendBroadcast(intent);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * 根据路径获取文件名
     *
     * @param imgPath
     * @return
     */
    private String getImgName(String imgPath) {
        String temp[] = imgPath.replaceAll("\\\\", "/").split("/");
        if (temp.length > 1) {
            return temp[temp.length - 1];
        } else {
            return imgPath;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("TAG", "onCreate");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("TAG", "onDestroy");
    }
}

接下来是 TestActivity 代码

public static final String UPLOAD_RESULT = "naple.test.UPLOAD_RESULT";
ArrayList<String> mDataList = new ArrayList<String>();
public class TestActivity extends Activity {  

    private BroadcastReceiver uploadImgReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == UPLOAD_RESULT) {
                String backResult = intent.getStringExtra(UploadImgService.BACK_RESULT);
                handleResult(backResult);
            }
        }
    };

    private void handleResult(String backResult) {
        //返回的结果处理
         System.out.println(TAG, "上传结果" + backResult);
    }
    
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);   
        registerReceiver();
        initView();
    }  
    public void initView() {
               // 布局文件就一个 button 
        Button mUpBtn = (Button) findViewById(R.id.btn_upload);
        mUpBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doLoad();
            }
        });
    }
    private void doLoad(){
        // 请求普通信息
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("username", "如果你需要传给服务端用户名的话");
        //mDataList 存放图片地址
        UploadImgService.startUploadImg(this, mDataList, params);

    }

    @Override
    protected void onDestroy() {    
          super.onDestroy();
          unregisterReceiver(uploadImgReceiver);             
    }
    private void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(UPLOAD_RESULT);
        registerReceiver(uploadImgReceiver, filter);    
    }
}

IntentService修改UI
IntentService如果要进行UI的修改,那么只能通过Handler来实现,或者使用广播机制来通知修改UI。
IntentService与AsyncTask的区别
对于异步更新UI来说,IntentService使用的是Serivce+handler或者广播的方式,而AsyncTask是thread+handler的方式
AsyncTask比IntentService更加轻量级一点。
Thread的运行独立于Activity,当Activity结束之后,如果没有结束thread,那么这个Activity将不再持有该thread的引用。
Service不能在onStart方法中执行耗时操作,只能放在子线程中进行处理,当有新的intent请求过来都会线onStartCommond将其入队列,当第一个耗时操作结束后,就会处理下一个耗时操作(此时调用onHandleIntent),都执行完了自动执行onDestory销毁IntengService服务。

有什么不足或者不对的地方,欢迎留言指正

httpmime-v4.2.3

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容