好久沒有更新博客了,而我並沒有放棄學習,放寒假之前急着考試,,,,放假後我回到家,卻沒網絡,所以就暫停了寫博客。但我利用寒假的期間將Android基礎學完了,跟着老師的視頻,把老師的代碼重新敲了一遍,雖然學得不是很精,但是基礎的知識點還是有了一個瞭解,四大組件,網絡,數據庫,UI,自定義組件我還沒學,,
今天我開始學習“安全衛士”因爲開學了就有網,連夜把視頻下載下來,今天趁着有空,學了第一天的課程。敲完了代碼……下面我把我做的(我照着老師的筆記做的)筆記,貼出來,供我以後複習的時候用,如果能幫到大家,那便是非常幸運的事。。。。
1、增加陰影效果
android:shadowColor="@android:color/holo_green_light"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
2、相對父佈局居中對齊
android:layout_centerInParent="true"
更改樣式-->改爲全屏
將android:theme="@android:style/Theme.Black.NoTitleBar"
中的<item name="windowNoTitle">true</item>屬性複製到
android:theme="@style/AppTheme"
中的style.xml文件中去
3、獲取版本號
private String getVersionCode() {
PackageManager pm = getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
String codeVerisonName = info.versionName;
return codeVerisonName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
4、連接服務器獲取新版本
private void update() {
//在子線程中更新服務器數據
new Thread() {
@Override
public void run() {
try {
URL url = new URL("xxxx");//設置連接的路徑
HttpURLConnection conn = (HttpURLConnection) url.openConnection();//獲取連接操作
conn.setConnectTimeout(5000);//設置超時時間
conn.setRequestMethod("GET");//設置連接服務器的方式
int responseCode = conn.getResponseCode();//設置狀態結果碼
if (responseCode == 200) {
//連接成功
} else {
//連接不成功
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
5、將服務器下載過來的json流信息轉換成String類型
public class stream {
public static String streamUtils(InputStream in) throws Exception {
//字符流,讀取流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
//寫入流
StringWriter sw = new StringWriter();
//寫入流的緩衝
String str = null;
while ((str = bufferedReader.readLine()) != null) {
//寫入操作
sw.write(str);
}
sw.close();
bufferedReader.close();
return sw.toString();
}
}
6、將轉換成String類型的json流,進行解析
String streamUtils = stream.streamUtils(strea);
JSONObject json = new JSONObject(streamUtils);
String code = json.getString("code");
String apkurl = json.getString("apkurl");
String des = json.getString("des");
Log.i("---->",code);
Log.i("---->",apkurl);
Log.i("---->",des);
7、設置提示更新對話框
[1]首先判斷版本號是否一致,不一致則使用message.obtain寫上一個標記
然後通過handle.sendmessage(meassge),發送給handle進程更新UI
if (code.equals(getVersionCode())) {
} else {
message.what = UPDATE_VERSION;
}
[2]在handle中更新UI(新建提示對話框)
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_VERSION:
showdialog();
}
}
};
private void showdialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("版本號:" + code);
builder.setIcon(R.drawable.ic_launcher);
builder.setMessage(des);
builder.setPositiveButton("升級", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
8、一些小細節
[1] 按取消按鈕後進入主頁面
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
enterHome();
}
});
private void enterHome() {
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
//進入主界面後再退出,直接退出到桌面
finish();
}
[2] 讓彈出的更細提示對話框,穩定出現在兩秒之後
int endTime= (int) System.currentTimeMillis();
//讓彈出的更細提示對話框,固定在兩秒鐘之後
if(endTime-startTime<2000){
SystemClock.sleep(2000-(endTime-startTime));
}
handler.sendMessage(message);
9、下載完成
private void download() {
//使用Xutils第三方jar包實現下載功能
HttpUtils httpUtils=new HttpUtils();
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
httpUtils.download(apkurl, "mnt/sdcard/app-debug.apk", new RequestCallBack<File>() {
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
}
@Override
public void onFailure(HttpException e, String s) {
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
tv_splash_download.setVisibility(View.VISIBLE);
tv_splash_download.setText(current+"/"+total);
}
});
}
}
我會利用好一切可以利用的時間來學習Android的,但可能做不到每天更新博客的速度,畢竟我還有課,算是對自己有個小小的交代吧。。。。。