編輯:關於Android編程
通過URL來獲取網絡資源並下載資源簡單實例:
package com.android.xiong.urltest;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView show;
Bitmap bitmap;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
// 使用ImageView顯示該圖片
show.setImageBitmap(bitmap);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (ImageView) findViewById(R.id.show);
new Thread() {
@Override
public void run() {
// 定義一個URL對象
URL url;
try {
url = new URL(
"http://img1.gtimg.com/news/pics/hv1/37/195/1468/95506462.jpg");
// 打開該URL的資源輸入流
InputStream is = url.openStream();
// 從InputStream中解析出圖片
bitmap = BitmapFactory.decodeStream(is);
// 發送消息
handler.sendEmptyMessage(0x123);
is.close();
// 再次打開RL對應的資源輸入流
is = url.openStream();
// 打開手機文件對應的輸出流
OutputStream os = openFileOutput("KEQIANG.JPG", MODE_APPEND);
byte[] buff = new byte[1024];
int hasRead = 0;
// 將URL資源下載到本地
while ((hasRead = is.read(buff)) > 0) {
os.write(buff, 0, hasRead);
}
is.close();
os.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
@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;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/hello_world"/>
</LinearLayout>
網絡資源多線程下載:
package com.example.threaddown;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
EditText url;
EditText target;
Button downBn;
ProgressBar bar;
DownUtil downUtil;
private int mDownStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 獲取程序界面中的三個界面控制
url = (EditText) findViewById(R.id.url);
target = (EditText) findViewById(R.id.target);
downBn = (Button) findViewById(R.id.downBn);
bar = (ProgressBar) findViewById(R.id.br);
// 創建一個Handler對象
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
bar.setProgress(mDownStatus);
}
}
};
downBn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 初始化DownUtil對象
downUtil = new DownUtil(url.getText().toString(), target
.getText().toString(), 6);
new Thread() {
@Override
public void run() {
try {
// 開始下載
downUtil.download();
} catch (Exception e) {
e.printStackTrace();
}
// 定義每秒調度獲取一次系統的完成進度
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// 獲取下載任務的完成比例
double completeRate = downUtil
.getCompleteRate();
mDownStatus = (int) (completeRate * 1000);
// 發送消息通知屆滿更新的進度條
handler.sendEmptyMessage(0x123);
// 下載完成之後取消任務進度
if (mDownStatus >= 100) {
timer.cancel();
}
}
}, 0, 1000);
}
}.start();
}
});
}
@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;
}
}
package com.example.threaddown;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class DownUtil {
// 定義下載資源的路徑
private String path;
// 指定所下載的文件的保存位置
private String targetFile;
// 定義需要使用多少線程下載資源
private int threadNum;
// 定義下載的線程對象
private DownThread[] threads;
// 定義下載的文件總大小
private int fileSize;
public DownUtil(String path, String targetFile, int threadNum) {
this.path = path;
this.targetFile = targetFile;
this.threadNum = threadNum;
}
public void download() throws IOException {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
// 得到文件的大小
fileSize = conn.getContentLength();
conn.disconnect();
int currentPartsSize = fileSize / threadNum + 1;
RandomAccessFile file = new RandomAccessFile(targetFile, "rw");
// 設置本地文件的大小
file.setLength(fileSize);
file.close();
for (int i = 0; i < threadNum; i++) {
// 計算每條線程的下載位置
int startPos = i * currentPartsSize;
// 每個線程使用一個RandomAccessFile進行下載
RandomAccessFile current = new RandomAccessFile(targetFile, "rw");
// 定義該線程的下載位置
current.seek(startPos);
// 創建下載線程
threads[i] = new DownThread(startPos, currentPartsSize, current);
// 啟動線程下載
threads[i].start();
}
}
// 獲取下載的完成百分比
public double getCompleteRate() {
// 統計多條線程已經下載的總大小
int sumSize = 0;
for (int i = 0; i < threadNum; i++) {
sumSize += threads[i].length;
}
return sumSize * 1.0 / fileSize;
}
private class DownThread extends Thread {
// 定義當前線程下載的位置
private int startPos;
// 定義當前線程下載文件的大小
private int currentPartsSize;
// 當前線程下載的文件塊
private RandomAccessFile currentPart;
// 定義該線程已下載的字節數
private int length;
public DownThread(int startPos, int currentPartsSize,
RandomAccessFile currentPart) {
this.startPos = startPos;
this.currentPart = currentPart;
this.currentPartsSize = currentPartsSize;
}
@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
InputStream in = conn.getInputStream();
in.skip(startPos);
int hasRead = 0;
byte[] buffer = new byte[1024];
// 讀取網絡數據,並寫入本地文件
while (length < currentPartsSize
&& (hasRead = in.read(buffer)) > 0) {
currentPart.write(buffer, 0, hasRead);
// 累計該線程下載的總大小
length += hasRead;
}
currentPart.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="http://ksoap2-android.googlecode.com/svn/m2-repo/com/google/code/ksoap2-android/ksoap2-android-assembly/3.1.0/ksoap2-android-assembly-3.1.0-jar-with-dependencies.jar" />
<EditText
android:id="@+id/target"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="/mnt/sdcard/"/>
<Button
android:id="@+id/downBn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="down" />
<ProgressBar
android:id="@+id/br"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- 在SD卡中創建與刪除文件的權限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 在SD開中寫入數據的權限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 訪問網路的權限 --> <uses-permission android:name="android.permission.INTERNET" />
android學習四(Activity的生命周期)
要學好活動(Activity),就必須要了解android中Activity的聲明周期,靈活的使用生命周期,可以開發出更好的程序,在android中是使用任務來管理活動的
解析android中include標簽的使用
在一個項目中我們可能會需要用到相同的布局設計,如果都寫在一個xml文件中,代碼顯得很冗余,並且可讀性也很差,所以我們可以把相同布局的代碼單獨寫成一個模塊,然後用到的時候可
Activity跳轉Flag你想知道的都在這了
Android 的一個特色就是 application A 的 activity 可以啟動 application B 的 activity,盡管 A 和 B 是毫無干系
android產品研發(九)--)App網絡傳輸協議
上一篇文章中我們講解了App的數據統計,其主要分為兩種:使用第三方服務統計和自身實現數據統計。一般而言我們使用第三方統計服務已經可以很好的滿足我們的也無需求了,只是部分數