Android 如何更新 app 的版本(中级) 版本更新 看看我们要用到哪些技术 1 自定义通知栏 2 HTTP 下载 3 AsyncTask 4 刷新通知栏中的进度条 5 执行 apk安装的隐士意图 6 Toast 7签名(安装时系统会自动检测签名是否一致) 8获得服务端和客户端的版本号 上代码 (1)点击事件判断是否有新版本更新(2)自定义一个通知 同时刷新下载进度 (3) 异步下载新版本app (4) 隐士意图来安装 首先别忘了写权限!!!!!超爱忘得东西 恨死它了 <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.INSTALL_PACKAGES"></uses-permission> private Runnable mrun; private NotificationManager mNotificationManager; private Notification notification; private RemoteViews remoteviews; private intcount; private int loadversion; private int version; //升级按钮点击事件的方法 通过服务器来解析JSON 得到版本号 判断是否来通知下载 private void upgrade() { PackageManager nPackageManager=getPackageManager();//得到包管理器 try { PackageInfo nPackageInfo=nPackageManager .getPackageInfo(getPackageName(),PackageManager. GET_CONFIGURATIONS ); loadversion=nPackageInfo.versionCode;//得到现在 app的版本号 } catch (NameNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //服务器端通过GET 得到JSON String json=JSandBitmap.httpGetDemo("http://192.168.14.234/version.json"); try { JSONArray jsonArray= new JSONArray(json); JSONObject jsonObject=jsonArray.getJSONObject(0); version = jsonObject.getInt("version");//得到服务端的 app版本号 } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } if((loadversion<version)){//如果有新版本发送通知开始下载 sendNotification(); } else{ //如果没有 弹出对话框告知用户 new AlertDialog.Builder( this) .setTitle("update cancel") .setMessage("Sorry Not new Version ") .setNegativeButton("cencel", null).show(); } } //此方法来发送下载通知 采用的是自定义通知栏 并且更加下载的进度来刷新进度条 //自定义通知的方法 在上上篇的博文中 这里不做太多的解释 private void sendNotification() { mNotificationManager = (NotificationManager) getSystemService(Context. NOTIFICATION_SERVICE); notification = new Notification( R.drawable. player_play_light, "Midiplay", System.currentTimeMillis()); remoteviews= new RemoteViews("com.tarena.gsd110623.midiplayonline", R.layout. mynotiifcation); remoteviews.setImageViewResource(R.id. imageView1, R.drawable. a00); notification.contentView=remoteviews; Intent intent= new Intent( this,install. class);//PendingIntent 调用的系统的安装隐士意图后面红色的代码 PendingIntent pendingintent=PendingIntent.getActivity( this, 0, intent, 0); notification.contentIntent=pendingintent; mrun= new Runnable() {//这个Runnable 用来根据下载进度来刷新进度条 @Override public void run() { if(count<98){//紫色的count 是异步下载计算出来设置进度的值 remoteviews.setProgressBar(R.id. progressBar1, 100, count, false); remoteviews.setTextViewText(R.id. textView1, count+"%"); mNotificationManager.notify(8888, notification); handler.postDelayed(mrun, 300); } else{//这里计算出来的count 不是那么准确 所以下载完成后 给一个固定值做为下载完成 remoteviews.setProgressBar(R.id. progressBar1, 100, 100, false); remoteviews.setTextViewText(R.id. textView1, 100+"%"); mNotificationManager.notify(8888, notification); Toast.makeText(Welcome. this, "download over", Toast. LENGTH_SHORT);//提示用户下载成功 } } }; handler.postDelayed(mrun, 300); Update_AsyncTask mUpdate_AsyncTask= new Update_AsyncTask(); try {//启动下载 mUpdate_AsyncTask.execute( new URL("http://192.168.14.234/android_project_midiplayonline.apk")); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //这个内部类用来异步下载新版本 app 通过服务端下载这里不多说了 class Update_AsyncTask extends AsyncTask<URL, Integer, Object>{ @Override protected Object doInBackground(URL... params) { // TODO Auto-generated method stub try { URLConnection con = params[0].openConnection(); if (HttpURLConnection. HTTP_OK != ((HttpURLConnection)con).getResponseCode()) { Log.i("Main", "connection failed"); return null; } InputStream is = con.getInputStream(); int contentlength=con.getContentLength();//得到下载的总长度 System. out.println(contentlength); File file= new File(Constant. APK_FILE_PATH); if(!file.exists()){ file.getParentFile().mkdirs(); file.createNewFile(); } FileOutputStream out= new FileOutputStream(file); int current = 0; int x=0; byte[]arr= new byte[1024]; while ( (current = is.read(arr)) != -1 ){ out.write(arr, 0, current); x=x+current; count=( int)(100*x/contentlength);//计算下载的百分百 } is.close(); } catch (Exception e) { } return null; } } /* * 此类为系统安装的隐士意图 */ public class install extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); Intent notify_Intent = new Intent(Intent.ACTION_VIEW); notify_Intent.setDataAndType(Uri.fromFile(new File(Constant.APK_FILE_PATH)), "application/vnd.android.package-archive"); startActivity(notify_Intent); //取消上一个通知 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.cancel(8888); overridePendingTransition(0, 0); finish(); } } 效果如下

更多相关文章

  1. android下载的进度条
  2. Android(安卓)8.0 设置默认闹钟提示音或者默认通知提示音
  3. android 简单例子
  4. Android(安卓)Studio更新升级方法
  5. android 开发书籍打包下载(包含十几本Android开发书籍)
  6. Ubuntu下建立Android开发环境
  7. Android(安卓)开发环境 adt-bundle android-studio sdk adt 下载
  8. ubuntu系统下,搭建Android开发环境!!
  9. android通知栏消息

随机推荐

  1. MySQL:ERROR 1227(42000):访问被拒绝 - 无法
  2. Centos安装完MariaDB后启动不了 MySQL is
  3. 在我的sql中调用触发器中的函数
  4. 【转】MySQL分库分表环境下全局ID生成方
  5. PHP和MySQL ACID程序设计
  6. 如何从表中获取每个id的最新状态?
  7. 获取具有行sql中的最大值的列名
  8. MySQL数据库 prompt参数详解
  9. MySql与SqlServer的一些常用用法的差别
  10. MySQL数据库之part1