




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
線程實現(xiàn)客戶端與服務(wù)器通信一、線程說明2一:在android中有兩種實現(xiàn)線程thread的方法:一種是,擴展java.lang.Thread類另一種是,實現(xiàn)Runnable接口二:Thread類代表線程類,它的兩個最主要的方法是:run()——包含線程運行時所執(zhí)行的代碼Start()——用于啟動線程三:Handler機制,它是Runnable和Activity交互的橋梁,在run方法中發(fā)送Message,在Handler里,通過不同的Message執(zhí)行不同的任務(wù)。二、代碼案例3主頁面mian_activity.xml<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下載圖片"
android:id="@+id/btn"/>
</LinearLayout>
二、代碼案例4privatestaticButtonbtn;
privatestaticContextcontext;
privatestaticStringfilePath;
privatestaticBitmapmBitmap;
privatestaticStringmSaveMessage="失敗";
privatefinalstaticStringTAG="PictureActivity";
privatestaticProgressDialogmSaveDialog=null;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewview){
donwloadImg(MainActivity.this,"21:8080/photo/test.jpg");
}
});
}主頁面MainActivity.java二、代碼案例5
publicstaticvoiddonwloadImg(Contextcontexts,StringfilePaths){
context=contexts;
filePath=filePaths;
mSaveDialog=ProgressDialog.show(context,"保存圖片","圖片正在保存中,請稍等...",true);
newThread(saveFileRunnable).start();
}
privatestaticRunnablesaveFileRunnable=newRunnable(){
@Override
publicvoidrun(){
try{
if(!TextUtils.isEmpty(filePath)){//網(wǎng)絡(luò)圖片
//對資源鏈接
URLurl=newURL(filePath);
//打開輸入流
InputStreaminputStream=url.openStream();
//對網(wǎng)上資源進行下載轉(zhuǎn)換位圖圖片
mBitmap=BitmapFactory.decodeStream(inputStream);
inputStream.close();
}
saveFile(mBitmap);
mSaveMessage="圖片保存成功!";
}catch(IOExceptione){
mSaveMessage="圖片保存失敗!";
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
messageHandler.sendMessage(messageHandler.obtainMessage());
}
};二、代碼案例6
privatestaticHandlermessageHandler=newHandler(){
@Override
publicvoidhandleMessage(Messagemsg){
mSaveDialog.dismiss();
Log.d(TAG,mSaveMessage);
Toast.makeText(context,mSaveMessage,Toast.LENGTH_SHORT).show();
}
};
/**
*保存圖片
*/
publicstaticvoidsaveFile(Bitmapbm)throwsIOException{
FiledirFile=newFile(Environment.getExternalStorageDirectory().getPath());
if(!dirFile.exists()){
dirFile.mkdir();
}
StringfileName=UUID.randomUUID().toString()+".jpg";
FilemyCaptureFile=newFile(Environment.getExternalStorageDirectory().getPath()+"/DCIM/Camera/"+fileName);
BufferedOutputStreambos=newBufferedOutputStream(newFileOutputStream(myCaptureFile));
press(Bitmap.CompressFormat.JPEG,80,bos);
bos.flush();
bos.close();
//把圖片保存后聲明這個廣播事件通知系統(tǒng)相冊有新圖片到來
Intentintent=newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uriuri=Uri.fromFile(myCaptureFile);
intent.setData(uri);
context.sendBroadcast(intent);
}
}二、代碼案例7<uses-permissionandroid:name="android.permission.INTERNET"></uses-perm
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- TY/T 1110-2024體育賽事活動參賽指引編制規(guī)范
- 科協(xié)課題立項申報書范文
- 如何撰寫社科課題申報書
- 叉車租賃帶司機合同范本
- 課題申報書哪里查
- 班級管理 課題立申報書
- 班級建設(shè)課題申報書
- 合同范本 銷售合同
- 周結(jié)算合同范例
- 深圳課題申報書格式
- GB/T 7251.3-2017低壓成套開關(guān)設(shè)備和控制設(shè)備第3部分:由一般人員操作的配電板(DBO)
- 工程質(zhì)量回訪記錄
- GB/T 2572-2005纖維增強塑料平均線膨脹系數(shù)試驗方法
- 2023年江蘇省中學(xué)生生物奧林匹克競賽試題及答案
- 維修質(zhì)量檢驗制度
- 食管支架植入術(shù)后護理課件
- 品質(zhì)控制計劃(QC工程圖)
- 海外派遣人員管理辦法
- 混凝土灌注樁質(zhì)量平行檢查記錄(鋼筋籠)
- 汽車營銷學(xué)(全套課件)
- 現(xiàn)澆墩臺身軸線偏位、全高豎直度檢測記錄表
評論
0/150
提交評論