d出升级对话框
获取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; java.io.IOException; java.io.inputStream; java.net.httpURLConnection; java.net.MalformedURLException; java.net.URL; net.tsz.afinal.Finalhttp; net.tsz.afinal.http.AJAXCallBack; org.Json.JsONException; org.Json.JsONObject; androID.app.Activity; androID.app.AlertDialog; androID.app.AlertDialog.Builder; androID.content.DialogInterface; androID.content.DialogInterface.OnClickListener; androID.content.Intent; androID.content.pm.PackageInfo; androID.content.pm.PackageManager; androID.net.Uri; androID.os.Bundle; androID.os.Environment; androID.os.Handler; androID.os.Message; androID.util.Log; androID.vIEw.animation.AlphaAnimation; androID.Widget.TextVIEw; androID.Widget.Toast; com.qingguow.mobilesafe.utils.StreamTools;public class SplashActivity extends Activity { private static final String TAG = "SplashActivity"; protected final int ENTER_HOME = 0int VERSION_UPDATE = 1int URL_ERROR = 2int NETWORK_ERROR = 3int JsON_ERROE = 4private TextVIEw tv_splash_version; String description; String apkurl; TextVIEw tv_show_progress; @OverrIDe 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() { handleMessage(androID.os.Message msg) { switch (msg.what) { case ENTER_HOME: enterHome(); break; VERSION_UPDATE: d窗提示 AlertDialog.Builder builder = new Builder(SplashActivity.this); builder.setTitle("提示更新"); builder.setMessage(description); builder.setPositivebutton("立即更新", OnClickListener() { @OverrIDe voID onClick(DialogInterface arg0,1)">int arg1) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { Finalhttp finalhttp= Finalhttp(); finalhttp.download(apkurl,Environment.getExternalStorageDirectory()+"/mobilesafe2.0.apk",1)">new AJAXCallBack<file>(){ 下载失败 @OverrIDe voID onFailure(Throwable t,1)"> errorNo,String strMsg) { t.printstacktrace(); Toast.makeText(getApplicationContext(),"下载失败",1).show(); .onFailure(t,errorNo,strMsg); enterHome(); } 正在下载voID onLoading(long count,1)">long current) { int precent=(int)(current*100/count); tv_show_progress.setText("正在下载:"+precent+"%"); .onLoading(count,current); } 下载成功 onSuccess(file t) { Intent intent= 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); .onSuccess(t); } }); } else { Toast.makeText(getApplicationContext(),"未检测到SD卡").show(); } } }); builder.setNegativebutton("稍后再说",1)"> arg1) { arg0.dismiss(); enterHome(); } }); builder.show(); URL_ERROR: Toast.makeText(getApplicationContext(),"URL错误",0).show(); enterHome(); NETWORK_ERROR: Toast.makeText(getApplicationContext(),"网络错误",1)"> JsON_ERROE: Toast.makeText(getApplicationContext(),"JsON解析错误",1)">; } } }; /** * 进入主页 */ enterHome() { Intent intent = new Intent(SplashActivity.this,HomeActivity.class); startActivity(intent); finish(); }; * 检查新版本 checkVersion() { Thread() { run() { long startTime = System.currentTimeMillis(); Message mes = Message.obtain(); URL url; try { url = 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 = JsONObject(result); String newVersion = (String) Json.get("version"); (newVersion.equals(getVersionname())) { 进入主界面 mes.what = ENTER_HOME; } { 版本更新 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; } (IOException e) { e.printstacktrace(); Log.i(TAG,"网络连接错误" NETWORK_ERROR; } (JsONException e) { e.printstacktrace(); Log.i(TAG,"JsON解析错误" JsON_ERROE; } finally { 延迟效果 long endTime = System.currentTimeMillis(); long dTime = endTime - startTime; if (dTime < 3000) { { Thread.sleep(3000 - dTime); } (InterruptedException e) { } } handler.sendMessage(mes); } }; }.start(); } 获得应用版本名称 String getVersionname() { PackageManager pm = getPackageManager(); { PackageInfo info = pm.getPackageInfo(getPackagename(),1)">); return info.versionname; } (Exception e) { e.printstacktrace(); return ""; } }}
总结
以上是内存溢出为你收集整理的[android] 手机卫士应用程序更新和签名全部内容,希望文章能够帮你解决[android] 手机卫士应用程序更新和签名所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)