启动service报异常
诸位大神,这段代码我已经打了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);
//创建文件信息对象
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";
//保存下载文件所到的路经
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;
//启动下载任务
// 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{
//链接网络文件
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;
}
//在本地创建文件
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();
}
}
}
}