Android项目实战手机安全卫士(02)
目录
- 项目结构图
- 源代码
- 运行结果
- 项目源代码
项目结构图
源代码
清单 01. SplashActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | package com.coderdream.mobilesafe.activity; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import org.xmlpull.v1.XmlPullParserException; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Window; import android.view.WindowManager; import android.view.animation.AlphaAnimation; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.coderdream.mobilesafe.R; import com.coderdream.mobilesafe.domain.UpdateInfo; import com.coderdream.mobilesafe.engine.UpdateInfoParser; public class SplashActivity extends Activity { private TextView tv_splash_version; private UpdateInfo info; private static final int GET_INFO_SUCCESS = 10 ; private static final int SERVER_ERROR = 11 ; private static final int SERVER_URL_ERROR = 12 ; private static final int PROTOCOL_ERROR = 13 ; private static final int IO_ERROR = 14 ; private static final int XML_PARSE_ERROR = 15 ; private static final int DOWNLOAD_SUCCESS = 16 ; private static final int DOWNLOAD_ERROR = 17 ; protected static final String TAG = "SplashActivity" ; private long startTime; private RelativeLayout rl_splash; private long endTime; private ProgressDialog pd; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case XML_PARSE_ERROR: Toast.makeText(getApplicationContext(), "xml解析错误" , Toast.LENGTH_LONG).show(); // loadMainUI(); break ; case IO_ERROR: Toast.makeText(getApplicationContext(), "I/O错误" , Toast.LENGTH_LONG).show(); // loadMainUI(); break ; case PROTOCOL_ERROR: Toast.makeText(getApplicationContext(), "协议不支持" , Toast.LENGTH_LONG).show(); // loadMainUI(); break ; case SERVER_URL_ERROR: Toast.makeText(getApplicationContext(), "服务器路径不正确" , Toast.LENGTH_LONG).show(); // loadMainUI(); break ; case SERVER_ERROR: Toast.makeText(getApplicationContext(), "服务器内部异常" , Toast.LENGTH_LONG).show(); // loadMainUI(); break ; case GET_INFO_SUCCESS: String serverversion = info.getVersion(); String currentversion = getVersion(); if (currentversion.equals(serverversion)) { Log.i(TAG, "版本号相同进入主界面" ); // loadMainUI(); } else { Log.i(TAG, "版本号不相同,升级对话框" ); showUpdateDialog(); } break ; case DOWNLOAD_SUCCESS: Log.i(TAG, "文件下载成功" ); // File file = (File) msg.obj; // installApk(file); break ; case DOWNLOAD_ERROR: Toast.makeText(getApplicationContext(), "下载数据异常" , Toast.LENGTH_LONG).show(); // loadMainUI(); break ; } }; }; /** * 显示升级提示的对话框 */ protected void showUpdateDialog() { // 创建了对话框的构造器 AlertDialog.Builder builder = new Builder( this ); // 设置对话框的提示内容 builder.setIcon(getResources().getDrawable(R.drawable.notification)); // 设置升级标题 builder.setTitle( "升级提示" ); // 设置升级提示内容 builder.setMessage(info.getDescription()); // 创建下载进度条 pd = new ProgressDialog(SplashActivity. this ); // 设置进度条在显示时的提示消息 pd.setMessage( "正在下载" ); // 指定显示下载进度条为水平形状 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 设置升级按钮 builder.setPositiveButton( "升级" , new OnClickListener() { public void onClick(DialogInterface dialog, int which) { // loadMainUI(); } }); builder.setNegativeButton( "取消" , new OnClickListener() { public void onClick(DialogInterface dialog, int which) { // loadMainUI(); } }); builder.create().show(); } @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // 设置为无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置为全屏模式 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_splash); rl_splash = (RelativeLayout) findViewById(R.id.rl_splash); tv_splash_version = (TextView) findViewById(R.id.tv_splash_version); tv_splash_version.setText( "版本号:" + getVersion()); AlphaAnimation aa = new AlphaAnimation( 0 .3f, 1 .0f); aa.setDuration( 2000 ); rl_splash.startAnimation(aa); // 1.连接服务器获取服务器上的配置信息. new Thread( new CheckVersionTask()) { }.start(); } /** * 连网检查应用的版本号与服务端上的版本号是否相同 * * @author Administrator * */ private class CheckVersionTask implements Runnable { public void run() { // 获取Sdcard下的config.xml文件,如果该文件不存在,那么将会自动创建该文件 SharedPreferences sp = getSharedPreferences( "config" , MODE_PRIVATE); // 由sp对象来获取autoupdate所对应的boolean值,如果该键不存在,默认返回true boolean autoupdate = sp.getBoolean( "autoupdate" , true ); // 自动更新没有开启 if (!autoupdate) { try { // 睡眠2秒钟的是为了播放动画 Thread.sleep( 2000 ); } catch (InterruptedException e) { e.printStackTrace(); } // 睡眠2秒钟播放动画完毕后进入程序主界面 // loadMainUI(); } startTime = System.currentTimeMillis(); Message msg = Message.obtain(); try { // 获取服务端的配置信息的连接地址 String serverurl = getResources().getString(R.string.serverurl); URL url = new URL(serverurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod( "GET" ); // 设置请求方式 conn.setConnectTimeout( 5000 ); int code = conn.getResponseCode(); // 获取响应码 if (code == 200 ) { // 响应码为200时,表示与服务端连接成功 InputStream is = conn.getInputStream(); info = UpdateInfoParser.getUpdateInfo(is); endTime = System.currentTimeMillis(); long resulttime = endTime - startTime; if (resulttime < 2000 ) { try { Thread.sleep( 2000 - resulttime); } catch (InterruptedException e) { e.printStackTrace(); } } msg.what = GET_INFO_SUCCESS; handler.sendMessage(msg); } else { // 服务器状态错误. msg.what = SERVER_ERROR; handler.sendMessage(msg); endTime = System.currentTimeMillis(); long resulttime = endTime - startTime; if (resulttime < 2000 ) { try { Thread.sleep( 2000 - resulttime); } catch (InterruptedException e) { e.printStackTrace(); } } } } catch (MalformedURLException e) { e.printStackTrace(); msg.what = SERVER_URL_ERROR; handler.sendMessage(msg); } catch (ProtocolException e) { msg.what = PROTOCOL_ERROR; handler.sendMessage(msg); e.printStackTrace(); } catch (IOException e) { msg.what = IO_ERROR; handler.sendMessage(msg); e.printStackTrace(); } catch (XmlPullParserException e) { msg.what = XML_PARSE_ERROR; handler.sendMessage(msg); e.printStackTrace(); } } } /** * <pre> * 获取当前应用程序的版本号。 * 版本号存在于我们的APK中对应的清单文件中(直接解压APK后,即可看到对应的清单文件), * 版本号是manifest节点中的android:versionName="1.0" * 当一个应用程序被装到手机后 ,该apk拷贝到手机的data/app目录下(也就是系统中), * 如图6。所以想得到版本号,我们需要拿到与系统相关的服务,就可以得到apk中的信息了 * * </pre> * * @return */ private String getVersion() { // 得到系统的包管理器。已经得到了apk的面向对象的包装 PackageManager pm = this .getPackageManager(); try { // 参数一:当前应用程序的包名 参数二:可选的附加消息,这里我们用不到 ,可以定义为0 PackageInfo info = pm.getPackageInfo(getPackageName(), 0 ); // 返回当前应用程序的版本号 return info.versionName; } catch (Exception e) { // 包名未找到的异常,理论上, 该异常不可能会发生 e.printStackTrace(); return "" ; } } } |
清单 02. UpdateInfo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.coderdream.mobilesafe.domain; public class UpdateInfo { private String version; private String description; private String apkurl; public String getVersion() { return version; } public void setVersion(String version) { this .version = version; } public String getDescription() { return description; } public void setDescription(String description) { this .description = description; } public String getApkurl() { return apkurl; } public void setApkurl(String apkurl) { this .apkurl = apkurl; } } |
清单 03. UpdateInfoParser.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.coderdream.mobilesafe.engine; import java.io.IOException; import java.io.InputStream; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.util.Xml; import com.coderdream.mobilesafe.domain.UpdateInfo; /** * * 解析XML数据 * */ public class UpdateInfoParser { /** * @param is * xml文件的输入流 * @return updateinfo的对象 * @throws XmlPullParserException * @throws IOException */ public static UpdateInfo getUpdateInfo(InputStream is) throws XmlPullParserException, IOException { // 获得一个Pull解析的实例 XmlPullParser parser = Xml.newPullParser(); // 将要解析的文件流传入 parser.setInput(is, "UTF-8" ); // 创建UpdateInfo实例,用于存放解析得到的xml中的数据,最终将该对象返回 UpdateInfo info = new UpdateInfo(); // 获取当前触发的事件类型 int type = parser.getEventType(); // 使用while循环,如果获得的事件码是文档结束的话,那么就结束解析 while (type != XmlPullParser.END_DOCUMENT) { if (type == XmlPullParser.START_TAG) { // 开始元素 if ( "version" .equals(parser.getName())) { // 判断当前元素是否是读者需要检索的元素,下同 // 因为内容也相当于一个节点,所以获取内容时需要调用parser对象的nextText()方法才可以得到内容 String version = parser.nextText(); info.setVersion(version); } else if ( "description" .equals(parser.getName())) { String description = parser.nextText(); info.setDescription(description); } else if ( "apkurl" .equals(parser.getName())) { String apkurl = parser.nextText(); info.setApkurl(apkurl); } } type = parser.next(); } return info; } } |
清单 03. config.xml
1 2 3 4 5 6 | <?xml version= "1.0" encoding= "utf-8" ?> <resources> <string name= "serverurl" >http: //192.168.0.101:8080/info.xml</string> </resources> |
运行结果
工程源代码
分类:
Android项目实战之安全卫士
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞