數(shù)據(jù)線丟了,不想花錢去買,在網(wǎng)上看了看,android手機(jī)居然可以通過wifi進(jìn)行程序的調(diào)試,太好了,自己搞了一下,雖然網(wǎng)上寫的很詳細(xì),但是還是有些問題,記錄下來,下次參考。
1.首先讓android手機(jī)監(jiān)聽指定的端口:
這一步需要使用shell,因此手機(jī)上要有終端模擬器,不過網(wǎng)上很多,隨便找個(gè)就行了,依次敲入下列幾行:
2 |
setprop service.adb.tcp.port 5555 |
2.手機(jī)連接wifi并確保手機(jī)和電腦連接同一個(gè)網(wǎng)絡(luò),記下手機(jī)的ip地址,假設(shè)為a.b.c.d
3.電腦上打開命令提示符,敲入以下命令:
4.配置成功,命令行顯示:“connected to a.b.c.d”,然后就可以調(diào)試程序了,^_^。
要關(guān)閉wifi調(diào)試,也很簡(jiǎn)單,只需要把端口號(hào)設(shè)置為-1,并且重復(fù)第一步即可。
DownloadManager是Android為開發(fā)者提供的一個(gè)后臺(tái)應(yīng)用組件,它通過Http層進(jìn)行文件的下載任務(wù).
1:使用
首先要在AndroidManifest.xml中申請(qǐng)?jiān)L問DownloadManager的權(quán)限
<permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
添加一個(gè)下載任務(wù):
ContentValues values = new ContentValues();
values.put(Downloads.URI, url);//指定下載地址
values.put(Downloads.COOKIE_DATA, cookie);//如果下載Server需要cookie,設(shè)置cookie
values.put(Downloads.VISIBILITY,Downloads.VISIBILITY_HIDDEN);//設(shè)置下載提示是否在屏幕頂部顯示
values.put(Downloads.NOTIFICATION_PACKAGE, getPackageName());//設(shè)置下載完成之后回調(diào)的包名
values.put(Downloads.NOTIFICATION_CLASS, DownloadCompleteReceiver.class.getName());//設(shè)置下載完成之后負(fù)責(zé)接收的Receiver,這個(gè)類要繼承 BroadcastReceiver
values.put(Downloads.DESTINATION,save_path);//設(shè)置下載到的路徑,這個(gè)需要在Receiver里自行處理
values.put(Downloads.TITLE,title);//設(shè)置下載任務(wù)的名稱
this.getContentResolver().insert(Downloads.CONTENT_URI, values);//將其插入到DownloadManager的數(shù)據(jù)庫中,數(shù)據(jù)庫會(huì)觸發(fā)修改事件,啟動(dòng)下載任務(wù)
2:如何為DownloadManager設(shè)置代理,比如Wap
values.put(Downloads.PROXY_HOST,"10.0.0.172");
values.put(Downloads.PROXY_PORT,"80");
3:如何在下載過程中監(jiān)聽下載任務(wù)
可以通過監(jiān)聽數(shù)據(jù)庫來實(shí)現(xiàn)
DownloadsChangeObserver mDownloadObserver=new DownloadsChangeObserver(Downloads.CONTENT_URI);
private class DownloadsChangeObserver extends ContentObserver {
public DownloadsChangeObserver(Uri uri) {
super(new Handler());
}
@Override
public void onChange(boolean selfChange) {
//查詢需要監(jiān)聽的字段
//比如要監(jiān)聽實(shí)時(shí)下載進(jìn)度,查看當(dāng)前下載狀態(tài):是否已經(jīng)斷開,或者下載失敗等等
StringBuilder wherequery = new StringBuilder(Downloads.TITLE);
wherequery.append("=");
wherequery.append("'");
wherequery.append(mTitle);
wherequery.append("'");
mDownloadCursor =mContext.getContentResolver().query(Downloads.CONTENT_URI, new String[] {Downloads.TITLE, Downloads.STATUS, Downloads.CURRENT_BYTES,}, wherequery.toString(), null,orderBy);
int mSizeColunmId=mDownloadCursor.getColumnIndexOrThrow(Downloads.CURRENT_BYTES);
mDownloadCursor.moveToFirst();
int size=mDownloadCursor.getInt(mSizeColunmId);
}
}
4:如何刪除下載記錄
private void deleteHistory(String title)//刪除掉指定名稱的下載記錄
{
StringBuilder whereDelete = new StringBuilder(Downloads.TITLE);
whereDelete.append("=");
whereDelete.append("'");
whereDelete.append(str);
whereDelete.append("'");
this.getContentResolver().delete(Downloads.CONTENT_URI,whereDelete.toString(), null);
}
https://github.com/commonsguy/cw-android/tree/master/Internet/Downloadhttp://hi-android.info/src/com/android/providers/downloads/DownloadProvider.java.html