亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

在service中調用子線程報異常


??? @Override
??? public int onStartCommand(Intent intent, int flags, int startId) {
?? ??? ?//實例化 MyHandler類
?? ??? ?myHandler=new MyHandler();
??????? if(ACTION_START.equals(intent.getAction())){?? ??? ?
?????? ??? ?FileInfo fileInfo=(FileInfo)intent.getSerializableExtra("fileInfo");
?????? ??? ?//啟動初始化線程
?????? ??? ?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{
?????????????? //鏈接網絡文件
?????????? ??? ?URL url=new URL(mFileInfo.geturl());
?????????? ??? ? conn=(HttpURLConnection)url.openConnection();
?????????? ??? ? conn.setConnectTimeout(5000);
???????????????? conn.setRequestMethod("GET");
??????????????? int length=-1;
??????????????? if(conn.getResponseCode()==206){
?????????????????? //獲得文件長度
?????????????? ??? ?length=conn.getContentLength();
??????????????? }
??????????????? if(length<=0){
?????????????? ??? ?return;
??????????????? }
??????????????? //在本地創建文件
??????????????? 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);
?????????????? ?
??????????????? //設置文件長度,用MyHandler的對象傳遞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();
?? ??? ??? ??? ?}
??????????? }
??????? }
??? }

?? ?
?? ?
???

正在回答

1 回答

http://img1.sycdn.imooc.com//5726c2df0001e47a13270388.jpg

每次都報這樣的異常,,,,和解呀

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android-Service系列之斷點續傳下載
  • 參與學習       20431    人
  • 解答問題       103    個

想升職加薪么?本章課程你值得擁有,滿滿的干貨,學起來吧

進入課程

在service中調用子線程報異常

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號