您的位置:首页 > 理论基础 > 计算机网络

Android开发之根据Service的生命周期特点后台连接网络下载图片(源代码分享)

2014-04-07 10:24 549 查看
       上一章讲到Service在后台启动后不会自动销毁掉,其销毁的方式有两种一个是在外部使用stopService()方法,一个就是在继承Service的类下调用stopSelf(),那么应该何时调用stopself()方法呢,如果不调用的话,service在后台会一直处在连接网络的状态,其内耗是可想而知的。这篇博文就会向大家介绍如果使用handle的信息传送机制来停止service的后台运行。‘

MainActivity

package com.example.f21_service01;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)this.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,DownLoadService.class);
startService(intent);//通过intent启动service
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

MainActivity中的代码很简单,通过按下按钮来启动一个service,在这里还得强调一下service要记得在清单文件中声明,要不然会报错的。
DownloadService的代码

package com.example.f21_service01;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class DownLoadService extends Service {
private static final String path = "http://my.csdn.net/u013900875";
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
stopSelf(); //当系统接收到消息后,关闭service
}

};
};

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
new Thread(new MyThread()).start();//启动线程
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}

public class MyThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(path);
try {
HttpResponse httpResponse = client.execute(httpPost);
byte[] result = EntityUtils.toByteArray(httpResponse
.getEntity());//使用http协议下载图片
boolean flag = SDcardtoFile.WriteToFile("hello", result);//当成功写入内存卡后,将标志设为true
if (flag) {

handler.sendEmptyMessage(1);//通过handler发送消息
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}

在startCommand()启动下载的线程,当下载完成后并且成功写入内存卡后,将flag置位true,通过handler的sendMessage()方法发送,然后调用stopself(),那为什么不直接调用这个方法呢,因为调用这个方法service的停止时间是不确定的,后面的代码还是会执行的。
最后再来回顾下之前前的sdcard的存入方法的书写

package com.example.f21_service01;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Environment;

public class SDcardtoFile {
public static boolean WriteToFile(String name, byte[] data) {
boolean flag = false;
String state = Environment.getExternalStorageState();
FileOutputStream fileOutputStream = null;
if (state.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(),
name);
try {
fileOutputStream=new FileOutputStream(file);
try {
fileOutputStream.write(data, 0, data.length);
flag=true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (fileOutputStream!=null) {
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}
return flag;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息