中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

Android利用后臺(tái)服務(wù)下載網(wǎng)絡(luò)數(shù)據(jù)

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用

/**

 * service運(yùn)行在主線程里所以不能使用HTTP協(xié)議訪問網(wǎng)絡(luò)

 * 

 * try catch的實(shí)例盡量在該塊外面定義

 */

public class MyService extends Service {

 

 

 

public MyService() {

// TODO Auto-generated constructor stub

}

 

@Override

public void onCreate() {

// TODO Auto-generated method stub

super.onCreate();

}

 

 

 

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

//獲得圖片地址

final String url=intent.getStringExtra("image_path");

final Handler handler=new Handler(){

 

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

if(msg.what==111)

{

Toast.makeText(MyService.this,"下載完成", Toast.LENGTH_LONG).show();

stopSelf();//startService()啟動(dòng)后,關(guān)閉service

}

}

};//注意分號(hào)

//啟動(dòng)線程訪問網(wǎng)絡(luò)

new Thread(new Runnable(){

 

@Override

public void run() {

//獲得獲取網(wǎng)絡(luò)資源的Http客戶端

HttpClient httpClient=new DefaultHttpClient(); 

//請(qǐng)求方式

HttpPost httpPost=new HttpPost(url);

HttpResponse httpResponse=null;

//網(wǎng)絡(luò)資源的字節(jié)數(shù)組

byte[] data=null;

//文件存儲(chǔ)路徑

File file=new File(Environment.getExternalStorageDirectory(),"圖片后臺(tái)下載.jpg");

FileOutputStream fileOutputStream=null;

try {

//執(zhí)行請(qǐng)求獲得響應(yīng)

httpResponse=httpClient.execute(httpPost);

//判斷響應(yīng)是否成功

if(httpResponse.getStatusLine().getStatusCode()==200)

{

//獲得內(nèi)容的字節(jié)數(shù)組

data=EntityUtils.toByteArray(httpResponse.getEntity());

//判斷SD卡是否可用

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))

{

fileOutputStream=new FileOutputStream(file);

fileOutputStream.write(data, 0,data.length);

//完成下載發(fā)送消息

Message message=Message.obtain();

message.what=111;

handler.sendMessage(message);//向主線程發(fā)送消息

}

}

} catch (ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException 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();

}

}

//關(guān)閉該進(jìn)程

if(httpClient!=null)

{

httpClient.getConnectionManager().shutdown();

}

}

}}).start();

return super.onStartCommand(intent, flags, startId);

}

 

 

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

@Override

public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

}

 

}

public class MainActivity extends Activity {

 

private String url="http://p16.qhimg.com/bdr/__85/d/_open360/fengjing0321/9.jpg";

private Button btnDownload=null;

private ImageView image=null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnDownload=(Button)this.findViewById(R.id.button1);

image=(ImageView)this.findViewById(R.id.imageView1);

btnDownload.setOnClickListener(new OnClickListener(){

 

@Override

public void onClick(View v) {

Intent intent=new Intent(MainActivity.this,MyService.class);

intent.putExtra("image_path",url);

startService(intent);

}

});

}

 

@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;

}

 

}

標(biāo)簽: 網(wǎng)絡(luò)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:百度語音識(shí)別API的python使用示例

下一篇:Java壓縮和解壓文件工具類ZipUtil