java手机杀毒_Android项目实战_手机安全卫士splash界面
- 根據代碼的類型組織包結構
1. 界面 com.hb.mobilesafe.activities
2. 服務 com.hb.mobilesafe.services
3. 業務邏輯 com.hb.mobilesafe.engine
4. 數據庫 com.hb.mobilesafe.db
5. 數據庫增刪改查 com.hb.mobilesafe.db.dao
6. 工具類 com.hb.mobilesafe.utils
7. 自定義view com.hb.mobilesafe.ui
###3.splash界面的作用
1. 展現產品的logo,提升產品的知名度.
2. 初始化應用程序的數據.
3. 連接服務器,查找可更新的版本,自動更新
4. 用戶操作指南
5. 新版本特性提醒
4.布局文件的命名規則
SplashActivity--->activity_spalsh.xml
XxxActivity---> activity_xxx.xml
###5.獲取應用程序版本號
//用PackageManager拿到PackageInfo,PackageInfo中的versionName
PackageInfo packinfo = context.getPackageManager().getPackageInfo(
context.getPackageName(), 0);
String version = packinfo.versionName;
###6.源代碼版本控制
- 安裝VisualSVN Server——SVN服務器,一直下一步即可
- 導入倉庫到服務器
1.在Repositories處右鍵,選擇Import Existing Repository
2.選擇Copy repository from another location,下一步
3.點擊Browse,選擇倉庫路徑,"代碼/代碼倉庫/Repository/mobilesafe",點擊下一步
4.點擊Import
5.點擊Finish,導入完成
- 安裝TortoiseSVN——SVN客戶端,一直下一步即可
1.在想要檢出代碼的地方右鍵,選擇SVN Checkout
2.URL of repository處填https://127.0.0.1/svn/mobilesafe/,地址也可以從SVN服務器的mobilesafe處右鍵選擇Copy URL to clipboard拷貝
3.Checkout directory出填寫檢出代碼要放的位置,然后點擊OK
4.完成代碼的檢出
- 將代碼更新到指定版本
1.mobilesafe文件夾出右鍵,選擇Update to version
2.點擊show log
3.點擊左下角的show all
4.選擇要更新的版本,點擊OK
5.版本更新完成
###7.應用自動更新的邏輯圖

###8.獲取服務器版本號
//獲取服務器地址
String path = getResources().getString(R.string.url);
URL url = new URL(path);
//創建網絡連接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
//發出請求,獲得返回碼
int code = conn.getResponseCode();
if(code ==200){
//獲取服務器返回的流并進行解析
InputStream is = conn.getInputStream();
String result = StreamTools.readStream(is);
//轉化為json并解析出版本號
JSONObject json = new JSONObject(result);
String serverVersion = json.getString("version");
Log.i(TAG,"服務器版本:"+serverVersion);
}
###9.將流轉化為字符串
public static String readStream(InputStream is) throws IOException{
//ByteArrayOutputStream類是在創建它的實例時,程序內部創建一個byte型數組的緩沖區,緩沖區會隨著數據的不斷寫入而自動增長。可使用 toByteArray()和 toString()獲取數據
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
is.close();
return baos.toString();
}
###10.彈出對話框
1.使用AlertDialog.Builder
2.設置標題、信息、點擊事件等
3.調用show方法顯示出來,調用dismiss方法消失
###11.下載apk
1.使用開源框架xUtils
2.使用HttpUtils的download方法,填入三個參數:服務器下載地址,手機中的存儲位置、回調事件
3.回調事件中有三個常用的方法:onSuccess下載成功、onFailure下載失敗、onLoading更新下載進度
xUtils補充
http://my.oschina.net/u/1171837/blog/147544 作者博客
###12.安裝apk
1.調用系統的安裝apk的界面,傳入對應的參數
2.具體實現方式
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(
Uri.fromFile(fileinfo.result),
"application/vnd.android.package-archive");
startActivity(intent);
###13.應用程序的覆蓋安裝要滿足的條件
1. 兩個版本簽名一致
2. 兩個版本包名一致
###14.跑馬燈效果的TextView
1. 系統的TextView不能獲取焦點,使用自定義控件
2. 繼承TextView控件,重寫isFocused方法,直接返回true,讓其獲取焦點
3. 設置android:ellipsize="marquee"
###15.旋轉的弘博logo
1. 使用系統提供的屬性動畫
2. 具體實現方式
ObjectAnimator oa = ObjectAnimator.ofFloat(iv_home_logo, "rotationY",
45, 90, 135, 180, 225, 270, 315);
oa.setDuration(3000);
oa.setRepeatCount(ObjectAnimator.INFINITE);
oa.setRepeatMode(ObjectAnimator.RESTART);
oa.start();
貼上對應的源碼:
1 packagecom.hb.mobilesafe.activities;2
3 importjava.io.File;4 importjava.io.FileOutputStream;5 importjava.io.IOException;6 importjava.io.InputStream;7 importjava.net.HttpURLConnection;8 importjava.net.URL;9
10 importorg.json.JSONObject;11
12 importandroid.app.Activity;13 importandroid.app.AlertDialog;14 importandroid.app.AlertDialog.Builder;15 importandroid.app.ProgressDialog;16 importandroid.content.DialogInterface;17 importandroid.content.DialogInterface.OnClickListener;18 importandroid.content.Intent;19 importandroid.content.SharedPreferences;20 importandroid.content.res.AssetManager;21 importandroid.net.Uri;22 importandroid.os.Bundle;23 importandroid.os.Environment;24 importandroid.os.Handler;25 importandroid.os.Message;26 importandroid.os.SystemClock;27 importandroid.view.Window;28 importandroid.widget.TextView;29 importandroid.widget.Toast;30
31 importcom.hb.demo_mobilesafe.R;32 importcom.hb.mobilesafe.utils.GetVersonName;33 importcom.hb.mobilesafe.utils.ReadStrem;34 importcom.lidroid.xutils.HttpUtils;35 importcom.lidroid.xutils.exception.HttpException;36 importcom.lidroid.xutils.http.ResponseInfo;37 importcom.lidroid.xutils.http.callback.RequestCallBack;38
39 public class SplashActivities extendsActivity {40 protected static final int SUCCESS = 1;41 protected static final int ERROR = 2;42 privateTextView tv_splash_version;43 privateString localVersion;44 privateString serviceVerson;45 privateString apkpath;46 privateString descritoin;47 private Handler handler=newHandler(){48 public voidhandleMessage(android.os.Message msg) {49 switch(msg.what) {50 caseSUCCESS:51 //判斷出的版本號不一樣進行升級
52 builderLoading();53 break;54
55 caseERROR:56 Toast.makeText(SplashActivities.this, "錯誤信息:"+msg.obj, 0).show();57 loadUI();58 break;59 }60 }61
62
63 };64
65 @Override66 protected voidonCreate(Bundle savedInstanceState) {67 super.onCreate(savedInstanceState);68 requestWindowFeature(Window.FEATURE_NO_TITLE);69 setContentView(R.layout.activity_splash);70 tv_splash_version =(TextView) findViewById(R.id.tv_splash_version);71 SharedPreferences sp=getSharedPreferences("config", MODE_PRIVATE);72 //獲取開關狀態判斷是否進行升級提示
73 boolean tag = sp.getBoolean("update", true);74 if(tag){75 initDate();76 }else{77 newThread(){78 public voidrun() {79 SystemClock.sleep(1500);80 loadUI();81 };82 }.start();83 }84 localVersion = GetVersonName.version(this);85 tv_splash_version.setText("版本號:"+localVersion);86 //初始化導入數據庫到手機
87 importDataBase("antivirus.db");88
89 }90 //初始化導入數據庫到手機
91 private voidimportDataBase(String fileName) {92 File file=newFile(getFilesDir(), fileName);93 if(file.exists() && file.length()>0){94 return;95 }96 try{97 AssetManager assets =getAssets();98 InputStream is=assets.open(fileName);99 FileOutputStream fs=newFileOutputStream(file);100 int len=-1;101 byte [] arr=new byte[1024];102 while ((len =is.read(arr))!=-1) {103 fs.write(arr, 0, len);104 }105 fs.close();106 is.close();107 } catch(IOException e) {108 //TODO Auto-generated catch block
109 e.printStackTrace();110 }111 }112
113 private voidinitDate() {114
115 newThread(){116
117 private longstart;118 privateMessage message;119 public voidrun() {120 String path=getResources().getString(R.string.path);121
122 try{123 URL url = newURL(path);124 HttpURLConnection conn =(HttpURLConnection) url.openConnection();125 conn.setConnectTimeout(5000);126 conn.setRequestMethod("GET");127 int code =conn.getResponseCode();128 if(code == 200){129 InputStream stream =conn.getInputStream();130 String json =ReadStrem.readSreamUtil(stream);131 JSONObject ob=newJSONObject(json);132 serviceVerson = ob.getString("verson");133 descritoin = ob.getString("descritoin");134 apkpath = ob.getString("apkpath");135 start=SystemClock.currentThreadTimeMillis();136 /**
137 * 進行本地版本與服務器版本的對比138 */
139 if(localVersion.equals(serviceVerson)){140 //不需要升級直接調轉141 //loadUI();
142 }else{143 message = newMessage();144 message.what=SUCCESS;145 handler.sendMessage(message);146
147
148 }149 }150
151 } catch(Exception e) {152 e.printStackTrace();153 message = newMessage();154 message.what=ERROR;155 message.obj="code:404";156 handler.sendMessage(message);157
158 }finally{159 long end=SystemClock.currentThreadTimeMillis();160 long et=end-start;161 if(et>2000){162 }else{163 SystemClock.sleep(2000-et);164 }165
166 }167 }168
169
170 }.start();171 }172 /**
173 * 更新UI174 */
175 private voidloadUI() {176 Intent intent=new Intent(SplashActivities.this, HomeActivities.class);177 startActivity(intent);178 finish();179 };180 //提示用戶升級
181 private voidbuilderLoading() {182 AlertDialog.Builder alert=new Builder(SplashActivities.this);183 alert.setTitle("版本升級提示");184 alert.setCancelable(false);//設置鎖定其他界面點擊事件
185 alert.setMessage(descritoin);186 //alert.setIcon(R.drawable.);
187 alert.setPositiveButton("立刻升級", newOnClickListener() {188
189 privateProgressDialog pd;190
191 @Override192 public void onClick(DialogInterface dialog, intwhich) {193 Toast.makeText(SplashActivities.this, "升級吧", 0).show();194 pd = new ProgressDialog(SplashActivities.this);195 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);196 pd.show();197
198 HttpUtils utils=newHttpUtils();199 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){200 //獲取SD卡的對象
201 File sd=Environment.getExternalStorageDirectory();202 File file=new File(sd, SystemClock.currentThreadTimeMillis()+".apk");203 utils.download(apkpath, file.getAbsolutePath(),new RequestCallBack(){204
205 @Override206 public voidonFailure(HttpException arg0, String arg1) {207 pd.dismiss();208 }209
210 @Override211 public void onSuccess(ResponseInfoarg0) {212 pd.dismiss();213 //安裝APP
214 Intent intent=newIntent();215 intent.setAction("android.intent.action.VIEW");216 intent.addCategory("android.intent.category.DEFAULT");217 intent.setDataAndType(218 Uri.fromFile(arg0.result),219 "application/vnd.android.package-archive");220 startActivity(intent);221 finish();222 }223
224 @Override225 public void onLoading(long total, longcurrent,226 booleanisUploading) {227 pd.setMax((int)total);228 pd.setProgress((int)current);229 super.onLoading(total, current, isUploading);230 }231 });232
233 }234 }235 });236 alert.setNegativeButton("下次再說", newOnClickListener() {237
238 @Override239 public void onClick(DialogInterface dialog, intwhich) {240 loadUI();241 }242 });243 alert.show();244
245 }246
247
248 }
布局文件:
android:id="@+id/tv_splash_version"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="15dip"android:textSize="16sp"android:text="版本號:1.0" />
給上這個項目中所需的所有權限不懂的可以百度一下:
總結
以上是生活随笔為你收集整理的java手机杀毒_Android项目实战_手机安全卫士splash界面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java布局垂直居中_CSS水平居中和垂
- 下一篇: android sina oauth2.