第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

啟動service報異常

諸位大神,這段代碼我已經(jīng)打了3次,每次都報異常(每次在啟動service的),和解呀?

MainActivity中:

public class MainActivity extends Activity {
??? private ProgressBar progressBar;
??? private Button mBtStart,mBtStop;
??? private static final String DOWNLOAD_PATH="kugoo://|Music|Chris Medina - What Are Words" +
?? ??? ??? ?"|3000271|88bf6bc1a381c384af8858c6efdc6585|/";
??? private static final String FileName="kugouSong.mp3";
??? @Override
??? protected void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.activity_main);
??????? progressBar=(ProgressBar)findViewById(R.id.progressBarid);
??????? progressBar.setMax(100);
??????? mBtStart=(Button)findViewById(R.id.btstart);
??????? mBtStop=(Button)findViewById(R.id.btstop);?? ?
??????? //創(chuàng)建文件信息對象
???????? final FileInfo fileInfo=new FileInfo(1,DOWNLOAD_PATH,FileName,0,0);
??????? mBtStart.setOnClickListener(new View.OnClickListener() {
?? ??? ??? ?public void onClick(View v) {
?????????????? Intent intent=new Intent(MainActivity.this,DownloadService.class);
???????????????? intent.setAction(DownloadService.ACTION_START);
???????????????? intent.putExtra("fileInfo",fileInfo);
??????????????? startService(intent);
?? ??? ??? ?}
?? ??? ?});
??????? mBtStop.setOnClickListener(new View.OnClickListener() {
?? ??? ??? ?public void onClick(View v) {
?????????????? Intent intent=new Intent(MainActivity.this,DownloadService.class);
???????????????? intent.setAction(DownloadService.ACTION_STOP);
???????????????? intent.putExtra("fileInfo",fileInfo);
??????????????? startService(intent);
?? ??? ??? ?}
?? ??? ?});

service類中:

public class DownloadService extends Service{
??? public static final String ACTION_START="ACTION_START";
??? public static final String ACTION_STOP="ACTION_STOP";
??? public static final String ACTION_UPDATE="ACTION_UPDATE";
??? //保存下載文件所到的路經(jīng)
??? public static final String SAVE_PATH=
?? ??? ??? ?Environment.getExternalStorageDirectory().getAbsolutePath()+"/downloads/";
??? public MyHandler myHandler;
??? public static final int MSG_INIT=0;
??? private DownloadTask mTask=null;
?? ?
?? ?@Override
?? ?public IBinder onBind(Intent intent) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?return null;
?? ?}
??? @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");
?????? ??? ?//啟動初始化線程
?????? ??? ?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();
??? }?? ?
??? //繼承 Handler
??? public class MyHandler extends Handler{
?? ??? ?@Override
?? ??? ?public void handleMessage(Message msg) {
?? ??? ??? ?// TODO Auto-generated method stub
?? ??? ??? ?super.handleMessage(msg);
?? ??? ??? ?switch(msg.what){
?? ??? ??? ?? case MSG_INIT:
?? ??? ??? ??? ?? FileInfo fileInfo=(FileInfo)msg.obj;
?? ??? ??? ??? ?? //啟動下載任務(wù)
//?? ??? ??? ??? ?? mTask=new DownloadTask(DownloadService.this,fileInfo);
//?? ??? ??? ??? ?? mTask.download();
?? ??? ??? ??? ? break;
?? ??? ??? ?}
?? ??? ?}
??? }
??? /*
???? * 初始化子線程
???? */
??? 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()==200){
?????????????????? //獲得文件長度
?????????????? ??? ?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è)置文件長度,用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();
?? ??? ??? ??? ?}
??????????? }
??????? }
??? }
???

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

正在回答

1 回答

諸位大神,這段我表述的不是很清楚,重新發(fā)了個帖子,給大家造成困擾,不好意思

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

舉報

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

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

進入課程

啟動service報異常

我要回答 關(guān)注問題
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號