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;
}
}
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;
}
}
相关文章推荐
- Android开发之异步任务加载网络图片并存储在sdcard中(源代码分享)
- [置顶] Android开发之java代码工具类。判断当前网络是否连接并请求下载图片
- Android开发之使用Handler封装下载图片工具类(源代码分享)
- Android开发之java代码工具类。判断当前网络是否连接并请求下载图片
- Android异步下载网络图片(其三:ExecutorService)
- android通过NDK连接网络下载图片到本地
- Android开发之使用Handler封装下载图片工具类(源码分享)
- Android异步下载网络图片(其三:ExecutorService)
- Android开发之异步下载网络图片并显示到UI界面
- android开发省电之--Determining and Monitoring the Connectivity Status(根据网络连接状况去省电)
- Android平台 用户可以根据自己输入的地址下载网络上的图片和网页信息
- Android开发利用Volley框架下载和缓存网络图片
- Android手机开发:网络连接-打开Url下载信息
- [置顶] 网络请求以及网络请求下载图片的工具类 android开发java工具类
- android开发----service下载图片
- Android开发之异步获取并下载网络资源-下载图片和下载文本内容
- 网络请求以及网络请求下载图片的工具类 android开发java工具类
- Android开发之Service与Activity数据交互(源代码分享)
- Android开发利用Volley框架下载和缓存网络图片
- Android开发下载网络图片到本地文件