弹出升级对话框

获取AlertDialog.Builder对象,通过new出来

调用Builder对象的setTitle()方法,参数:文本

调用Builder对象的setMessage()方法,参数:文本,json传回来的信息

调用Builder对象的setPositiveButton()方法,设置确定按钮

参数:文本,OnClickListener对象,匿名内部类实现,重写onClick()方法,

方法里面下载APK,替换安装

调用Builder对象的setNegativeButton()方法,参数和上面一样,点击后

关闭对话框调用AlertDialog对象的dismiss()方法,

跳转到主页

调用Builder对象的show()方法,显示出来

替换安装

检查sdcard是否存在,

调用Environment.getExternalStorageState()方法,返回状态,

判断Environment.MEDIA_MOUNTED,如果不想等提示一下,如果相等就下载APK

下载APK

使用第三方库,afinal,拷贝到项目的libs目录下面

实例化FinalHttp对象,通过new出来

调用FinalHttp对象的download(url,target,callback)方法,下载文件,

参数:url是路径,target是保存文件路径,callback是AjaxCallback对象

保存路径:Environment.getExternalStorageDirectory()+"/mobilesafe2.0.apk"

AjaxCallback是一个接口,通过new它实现接口,

重写以下方法,onFailure(),onSuccess(),onLoading()

下载失败

显示错误信息,吐司

正在下载 onLoading(long count,long current)

显示进度,在布局文件中添加一个TextView显示进度,位于左下角

在onLoading()方法里,计算拼接好setText()显示出来

下载成功 onSuccess()

安装apk,发送隐式意图,获取Intent对象,通过new

调用Intent对象的setAction()方法,设置动作,参数:android.intent.action.VIEW

调用Intent对象的addCategory()添加类型,参数:android.intent.category.DEFAULT

调用Intent对象的setDataAndType(),设置数据和类型,参数:Uri对象,

application/vnd.android.package-archive

Uri对象通过Uri.fromfile(),从文件中湖区Uri对象,参数是FIle对象

调用startActivity(intent),开启

需要写SD卡的权限,android.permission.WRITE_EXTERNAL_STORAFE

签名

在我们的android系统中,不允许安装两个包名相同的应用

例如:

A程序员开发了一款应用 com.tsh.A

B程序员开发了一款应用 com.tsh.A

包名相同,签名相同,可以替换安装

包名相同,签名不相同,安装失败

项目上右键 ==> export ==> Export Android Application ==>

create new keystore ==>

location 选中一个空文件最好命名 xxx.keystore

password 写上密码

confirm 重复密码==>

Alias 例如:mobilesafe

Password 刚才填的密码

Confirm 重复密码

Validity(years) 有效年份,过2030年,例如:40

下面的不重要,按自己的填 ==>

Destination APK file APK的保存路径

生成一个apk,生成了一个keystore文件(非常重要),设置的密码一定要记住

第二次导出的时候,

选择Use existing keystore

Password 写上刚才填的

代码:

package com.qingguow.mobilesafe;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack; import org.json.JSONException;
import org.json.JSONObject; import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;
import android.widget.Toast; import com.qingguow.mobilesafe.utils.StreamTools; public class SplashActivity extends Activity {
private static final String TAG = "SplashActivity";
protected static final int ENTER_HOME = 0;
protected static final int VERSION_UPDATE = 1;
protected static final int URL_ERROR = 2;
protected static final int NETWORK_ERROR = 3;
protected static final int JSON_ERROE = 4;
private TextView tv_splash_version;
private String description;
private String apkurl;
private TextView tv_show_progress; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
tv_splash_version = (TextView) findViewById(R.id.tv_splash_version);
tv_splash_version.setText("版本号" + getVersionName());
// 检查更新
checkVersion();
// 界面动画
AlphaAnimation aa = new AlphaAnimation(0.2f, 1.0f);
aa.setDuration(1000);
findViewById(R.id.rl_splash_root).setAnimation(aa);
tv_show_progress=(TextView) findViewById(R.id.tv_show_progress);
} private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case ENTER_HOME:
enterHome();
break;
case VERSION_UPDATE:
// 弹窗提示
AlertDialog.Builder builder = new Builder(SplashActivity.this);
builder.setTitle("提示更新");
builder.setMessage(description); builder.setPositiveButton("立即更新", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
FinalHttp finalhttp=new FinalHttp();
finalhttp.download(apkurl,Environment.getExternalStorageDirectory()+"/mobilesafe2.0.apk",new AjaxCallBack<File>(){
//下载失败
@Override
public void onFailure(Throwable t, int errorNo,
String strMsg) {
t.printStackTrace();
Toast.makeText(getApplicationContext(), "下载失败",
1).show();
super.onFailure(t, errorNo, strMsg);
enterHome();
}
//正在下载
@Override
public void onLoading(long count, long current) {
int precent=(int)(current*100/count);
tv_show_progress.setText("正在下载:"+precent+"%");
super.onLoading(count, current);
}
//下载成功
@Override
public void onSuccess(File t) {
Intent intent=new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.fromFile(t), "application/vnd.android.package-archive");
startActivity(intent);
super.onSuccess(t);
} });
} else {
Toast.makeText(getApplicationContext(), "未检测到SD卡",
1).show();
} }
});
builder.setNegativeButton("稍后再说", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
enterHome();
}
});
builder.show();
break;
case URL_ERROR:
Toast.makeText(getApplicationContext(), "URL错误", 0).show();
enterHome();
break;
case NETWORK_ERROR:
Toast.makeText(getApplicationContext(), "网络错误", 0).show();
enterHome();
break;
case JSON_ERROE:
Toast.makeText(getApplicationContext(), "JSON解析错误", 0).show();
enterHome();
break; }
} }; /**
* 进入主页
*/
private void enterHome() {
Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}; /**
* 检查新版本
*/
private void checkVersion() {
new Thread() {
public void run() {
long startTime = System.currentTimeMillis();
Message mes = Message.obtain();
URL url;
try {
url = new URL(getString(R.string.serverurl));
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(4000);
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
String result = StreamTools.readInputStream(is);
JSONObject json = new JSONObject(result);
String newVersion = (String) json.get("version");
if (newVersion.equals(getVersionName())) {
// 进入主界面
mes.what = ENTER_HOME;
} else {
// 版本更新
mes.what = VERSION_UPDATE;
description = (String) json.get("description");
apkurl = (String) json.get("apkurl");
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
Log.i(TAG, "URL错误");
mes.what = URL_ERROR;
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "网络连接错误");
mes.what = NETWORK_ERROR;
} catch (JSONException e) {
e.printStackTrace();
Log.i(TAG, "JSON解析错误");
mes.what = JSON_ERROE;
} finally {
// 延迟效果
long endTime = System.currentTimeMillis();
long dTime = endTime - startTime;
if (dTime < 3000) {
try {
Thread.sleep(3000 - dTime);
} catch (InterruptedException e) {
}
}
handler.sendMessage(mes);
} };
}.start(); } // 获得应用版本名称
private String getVersionName() {
PackageManager pm = getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
return info.versionName;
} catch (Exception e) {
e.printStackTrace();
return "";
} }
}

[android] 手机卫士应用程序更新和签名的更多相关文章

  1. [android] 手机卫士关闭自动更新

    保存数据的四种方式,网络,广播提供者,SharedPreferences,数据库 获取SharedPreferences对象,通过getSharedPreferences()方法,参数:名称,模式 例 ...

  2. Android小项目之十 应用程序更新的签名问题

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点 ...

  3. Android 手机卫士--签名文件说明&包名说明

    在<Android 手机卫士--打包生成apk维护到服务器>一文中,实现了新版本的apk到服务器,当打开客户端apk的时候,发现有新版本,提示更新.还实现了利用xutils工具实现了从服务 ...

  4. Android 手机卫士--弹出对话框

    在<Android 手机卫士--解析json与消息机制发送不同类型消息>一文中,消息机制发送不同类型的信息还没有完全实现,在出现异常的时候,应该弹出吐司提示异常,代码如下: private ...

  5. android手机卫士、3D指南针、动画精选、仿bilibli客户端、身份证银行卡识别等源码

    Android精选源码 android身份证.银行卡号扫描源码 android仿bilibili客户端 android一款3D 指南针 源码 android手机卫士app源码 android提醒应用, ...

  6. Android 手机卫士--参照文档编写选择器

    本文来实现<Android 手机卫士--导航界面1的布局编写>中的图片选择器部分的代码. 本文地址:http://www.cnblogs.com/wuyudong/p/5944356.ht ...

  7. Android 手机卫士--设置界面&功能列表界面跳转逻辑处理

    在<Android 手机卫士--md5加密过程>中已经实现了加密类,这里接着实现手机防盗功能 本文地址:http://www.cnblogs.com/wuyudong/p/5941959. ...

  8. Android 手机卫士--确认密码对话框编写

    本文接着实现“确认密码”功能,也即是用户以前设置过密码,现在只需要输入确认密码 本文地址:http://www.cnblogs.com/wuyudong/p/5940718.html,转载请注明出处. ...

  9. Android 手机卫士--阶段小结1

    本文地址:http://www.cnblogs.com/wuyudong/p/5904528.html,转载请注明源地址. 本文对之前手机卫士开发进行一个小结. 1.SplashActivity 版本 ...

随机推荐

  1. ASM的一些小坑

    变量必需放到数据段,才有直接对地址赋值的访问权限 segment .data n1 dw 55h segment .text global _nasm_function _nasm_function: ...

  2. sql server常用字符串函数

    --返回字符表达式中最左侧字符的ASCII代码值 --将整数ASCII代码转换为字符 )--a )--A ')--A SELECT CHAR('A')--在将 varchar 值 'A' 转换成数据类 ...

  3. Hadoop 系列文章(三) 配置部署启动YARN及在YARN上运行MapReduce程序

    这篇文章里我们将用配置 YARN,在 YARN 上运行 MapReduce. 1.修改 yarn-env.sh 环境变量里的 JAVA_HOME 路径 [bamboo@hadoop-senior ha ...

  4. 冲刺博客NO.8

    今天做了什么: 多天学习后,实现了短信验证的功能,可以选择国家,可以在Mob的后台管理短信验证 遇到的困难: 注册回调事件,afterEvent的判定(事件完成后调用)

  5. Java设计模式----中介者模式

    说到中介大家都不会陌生,买房子租房子有中介,出国留学有中介,买卖二手车还是有中介.那么中介到底是个什么角色呢?实际上,中介就是让买卖双方不必面对面直接交流,由他/她来完成买卖双方的交易,达到解耦买卖人 ...

  6. Linux pwn入门教程(2)——shellcode的使用,原理与变形

    作者:Tangerine@SAINTSEC 0×00 shellcode的使用 在上一篇文章中我们学习了怎么使用栈溢出劫持程序的执行流程.为了减少难度,演示和作业题程序里都带有很明显的后门.然而在现实 ...

  7. JavaCV 学习(一):JavaCV 初体验

    最近工作中因为项目的原因边学边用,接触并使用JavaCV做了一些相关的产品,目前觉得,JavaCV在图形图像(人家本身就是一个视觉库)的功能真的挺强大,而且使用起来避免了复杂的平台处理.下面整理一下对 ...

  8. 使用广播-BroadcastReceiver最详细解析

    女孩:BroadcastReceiver是什么呀? 男孩:Broadcast是广播的意思,在Android中应用程序之间的传输信息的机制,BroadcastReceiver是接收广播通知的组件,广播和 ...

  9. js生成随机固定长度字符串的简便方法

    概述 碰到一个需求:用js生成固定长度的字符串.在网上查了很多资料,网上的方法都比较麻烦.我自己灵光一现,实现了一个比较简单的方法.记录下来,供以后开发时参考,相信对其他人也有用. js生成随机字符串 ...

  10. ElasticSearch核心知识总结(一)es的六种搜索方式和数据分析

    es的六种搜索方式 query string search GET /ecommerce/product/_search //查询所有数据 { "took": 4,//耗费几毫秒 ...