在service中調(diào)用子線程報(bào)異常
??? @Override
??? public int onStartCommand(Intent intent, int flags, int startId) {
?? ??? ?//實(shí)例化 MyHandler類
?? ??? ?myHandler=new MyHandler();
??????? if(ACTION_START.equals(intent.getAction())){?? ??? ?
?????? ??? ?FileInfo fileInfo=(FileInfo)intent.getSerializableExtra("fileInfo");
?????? ??? ?//啟動(dòng)初始化線程
?????? ??? ?Log.d("huefew","正常");
?????? ??? ?new InitThread(fileInfo).start();
??????? }else if(ACTION_STOP.equals(intent.getAction())){
?????? ??? ?FileInfo fileInfo=(FileInfo)intent.getSerializableExtra("fileInfo");
?????? ??? ?if(mTask!=null){
?????? ??? ??? ?mTask.isPause=true;
?????? ??? ?}
??????? }
?? ??? ?return super.onStartCommand(intent, flags, startId);
??? }
??? @Override
??? public void onDestroy() {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?super.onDestroy();
??? }?? ?
???
??? /*
???? * 初始化子線程
???? */
??? class InitThread extends Thread{
?? ??? ?private FileInfo mFileInfo=null;
?? ??? ?public InitThread(FileInfo mfileInfo){
?? ??? ???? this.mFileInfo=mfileInfo;
?? ??? ?}
??????? public void run() {
?????? ??? ?HttpURLConnection conn=null;
?????? ??? ?RandomAccessFile raf=null;
??????????? try{
?????????????? //鏈接網(wǎng)絡(luò)文件
?????????? ??? ?URL url=new URL(mFileInfo.geturl());
?????????? ??? ? conn=(HttpURLConnection)url.openConnection();
?????????? ??? ? conn.setConnectTimeout(5000);
???????????????? conn.setRequestMethod("GET");
??????????????? int length=-1;
??????????????? if(conn.getResponseCode()==206){
?????????????????? //獲得文件長(zhǎng)度
?????????????? ??? ?length=conn.getContentLength();
??????????????? }
??????????????? if(length<=0){
?????????????? ??? ?return;
??????????????? }
??????????????? //在本地創(chuàng)建文件
??????????????? File dir=new File(SAVE_PATH);
??????????????? if(!dir.exists()){
?????????????? ??? ?dir.mkdir();
??????????????? }
??????????????? File file=new File(dir,mFileInfo.getfileName());
??????????????? raf=new RandomAccessFile(file,"rwd");
??????????????? raf.setLength(length);
?????????????? ?
??????????????? //設(shè)置文件長(zhǎng)度,用MyHandler的對(duì)象傳遞Message到被繼承的Handler方法中
??????????????? mFileInfo.setlength(length);
??????????????? Message message=Message.obtain();
???????????????? message.what=MSG_INIT;
???????????????? message.obj=mFileInfo;
???????????????? myHandler.sendMessage(message);
??????????? }catch(Exception e){
?????????? ??? ?e.printStackTrace();
??????????? }finally{
?????????? ??? ?try {
?? ??? ??? ??? ??? ?raf.close();
?? ??? ??? ??? ??? ?conn.disconnect();
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
??????????? }
??????? }
??? }
?? ?
?? ?
???
2016-05-02
每次都報(bào)這樣的異常,,,,和解呀