Android技术知识移动开发Android开发

IntentService 上传多图

2016-05-09  本文已影响681人  NapleC

如果没兴趣的可以直接看实例。
首先把 原文 搬过来: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

上一篇下一篇

猜你喜欢

热点阅读