- 浏览: 37350 次
- 性别:
- 来自: 厦门
文章分类
最新评论
package com.example.version_upgrade;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
private int versionCode_self;
private static final int UPGRADE = 0;
private String url;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PackageInfo packageInfo;
try
{
//得到当前应用的版本
packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
versionCode_self=packageInfo.versionCode;
//查看市场的最新版本 ,apk都有 说明书,先要读说明书的内容 ,版本号,与自己 的版本号比较
String url="http://169.254.104.44:8080/version.txt";
new DownSpecifications().execute(url);
}
catch (NameNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//下载apk的说明 书,做activity的内部类,是可以掉activity的方法 showDialog(0, null);
class DownSpecifications extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
InputStream is =null;
try
{
URL url = new URL(params[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
if(con.getResponseCode()!=HttpURLConnection.HTTP_OK)
{
Log.e("HttpURLConnection", "连接异常");
return null;
}
is = con.getInputStream();
byte[] buffer=new byte[1024];
int len=0;
StringBuilder sb = new StringBuilder();
while(-1!=(len=is.read(buffer)))
{
sb.append(new String(buffer, 0, len));
}
return sb.toString();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(is!=null)
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result)
{
try
{
JSONObject jsonObject = new JSONObject(result);
int versionCode = jsonObject.getInt("versionCode");
url = jsonObject.getString("url");
//内部类的好处,又可以拿activity的versionCode来比较,外部类就要通过构造函数传了
if(versionCode_self<versionCode)
showDialog(UPGRADE, null);
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected Dialog onCreateDialog(int id, Bundle args)
{
switch (id)
{
case UPGRADE:
Dialog dialog = new Dialog(this);
dialog.setTitle("有最新版本,是否要升级");
View view = getLayoutInflater().inflate(R.layout.upgrade, null);
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
dismissDialog(UPGRADE);
//启动一个服务来下载,notification里显示下载进度,如果在activity里显示进度就影响用户操作体验不好
//在此要传个要下载apk的地址 ,在读apk说明书的时候得到
Intent intent=new Intent(MainActivity.this, MyService.class);
intent.putExtra("url", url);
startService(intent);
}
});
dialog.setContentView(view);
return dialog;
default:
break;
}
return super.onCreateDialog(id, args);
}
}
///////////////////////////////////////////////////////////////////
package com.example.version_upgrade;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ProgressBar;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MyService extends Service
{
private Notification notification;
private ProgressBar progress;
private RemoteViews remoteViews;
private String apkName;
private NotificationManager manager;
public MyService()
{
}
@Override
public void onCreate()
{
//请求网络前,下make notification
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification=new Notification(R.drawable.ic_launcher, "通知", System.currentTimeMillis());
//flags 设为FLAG_NO_CLEAR 就不会消失
notification.flags=Notification.FLAG_NO_CLEAR;
remoteViews=new RemoteViews(getPackageName(), R.layout.notification);
//false是说明进度条不是无限进度 ,进度就100
remoteViews.setProgressBar(R.id.progressBar1, 100, 0, false);
notification.contentView=remoteViews;
Intent intent1=new Intent(this, MainActivity.class);
notification.contentIntent=PendingIntent.getActivity(this, 0, intent1, 0);
manager.notify(R.drawable.ic_launcher, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
String url = intent.getStringExtra("url");
getAPKname(url);
new DowmAPK().execute(url);
return super.onStartCommand(intent, flags, startId);
}
private void getAPKname(String url)
{
int lastIndexOf = url.lastIndexOf('/');
apkName = url.substring(lastIndexOf+1);
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
class DowmAPK extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params)
{
InputStream is =null;
FileOutputStream fos=null;
try
{
URL url = new URL(params[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
if(con.getResponseCode()!=HttpURLConnection.HTTP_OK)
{
Log.e("HttpURLConnection", "连接异常");
return null;
}
int contentLength = con.getContentLength();
is = con.getInputStream();
fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+apkName);
byte[] buffer=new byte[1024];
int len=0;
int len2=0;
StringBuilder sb = new StringBuilder();
while(-1!=(len=is.read(buffer)))
{
len2+=len;
publishProgress(len2,contentLength);
fos.write(buffer, 0, len);
}
return "down ok!!!";
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(is!=null)
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(fos!=null)
try
{
fos.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values)
{
Log.e("onProgressUpdate", "onProgressUpdate");
//values[1] 总字节 values[0]已经读了的字节
remoteViews.setProgressBar(R.id.progressBar1, values[1], values[0], false);
Log.e("onProgressUpdate22", "onProgressUpdate22");
// Post a notification to be shown in the status bar.
//If a notification with the same id has already been posted by your application
//and has not yet been canceled,
//it will be replaced by the updated information.
//更新notification的内容,一定要通知,就是调下面的方法,不然进度条不会更新的
manager.notify(R.drawable.ic_launcher, notification);
}
@Override
protected void onPostExecute(String result)
{
if(result!=null)
{
notification.flags=Notification.FLAG_AUTO_CANCEL;
manager.notify(R.drawable.ic_launcher, notification);
Toast.makeText(getApplication(), "下载完毕", Toast.LENGTH_LONG).show();
String fileName = Environment.getExternalStorageDirectory() +"/"+apkName;
//启动系统的activity
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)),"application/vnd.android.package-archive");
//不是在activity里 要加这句话
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
private int versionCode_self;
private static final int UPGRADE = 0;
private String url;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PackageInfo packageInfo;
try
{
//得到当前应用的版本
packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
versionCode_self=packageInfo.versionCode;
//查看市场的最新版本 ,apk都有 说明书,先要读说明书的内容 ,版本号,与自己 的版本号比较
String url="http://169.254.104.44:8080/version.txt";
new DownSpecifications().execute(url);
}
catch (NameNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//下载apk的说明 书,做activity的内部类,是可以掉activity的方法 showDialog(0, null);
class DownSpecifications extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params)
{
InputStream is =null;
try
{
URL url = new URL(params[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
if(con.getResponseCode()!=HttpURLConnection.HTTP_OK)
{
Log.e("HttpURLConnection", "连接异常");
return null;
}
is = con.getInputStream();
byte[] buffer=new byte[1024];
int len=0;
StringBuilder sb = new StringBuilder();
while(-1!=(len=is.read(buffer)))
{
sb.append(new String(buffer, 0, len));
}
return sb.toString();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(is!=null)
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result)
{
try
{
JSONObject jsonObject = new JSONObject(result);
int versionCode = jsonObject.getInt("versionCode");
url = jsonObject.getString("url");
//内部类的好处,又可以拿activity的versionCode来比较,外部类就要通过构造函数传了
if(versionCode_self<versionCode)
showDialog(UPGRADE, null);
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected Dialog onCreateDialog(int id, Bundle args)
{
switch (id)
{
case UPGRADE:
Dialog dialog = new Dialog(this);
dialog.setTitle("有最新版本,是否要升级");
View view = getLayoutInflater().inflate(R.layout.upgrade, null);
Button button = (Button) view.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
dismissDialog(UPGRADE);
//启动一个服务来下载,notification里显示下载进度,如果在activity里显示进度就影响用户操作体验不好
//在此要传个要下载apk的地址 ,在读apk说明书的时候得到
Intent intent=new Intent(MainActivity.this, MyService.class);
intent.putExtra("url", url);
startService(intent);
}
});
dialog.setContentView(view);
return dialog;
default:
break;
}
return super.onCreateDialog(id, args);
}
}
///////////////////////////////////////////////////////////////////
package com.example.version_upgrade;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ProgressBar;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MyService extends Service
{
private Notification notification;
private ProgressBar progress;
private RemoteViews remoteViews;
private String apkName;
private NotificationManager manager;
public MyService()
{
}
@Override
public void onCreate()
{
//请求网络前,下make notification
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification=new Notification(R.drawable.ic_launcher, "通知", System.currentTimeMillis());
//flags 设为FLAG_NO_CLEAR 就不会消失
notification.flags=Notification.FLAG_NO_CLEAR;
remoteViews=new RemoteViews(getPackageName(), R.layout.notification);
//false是说明进度条不是无限进度 ,进度就100
remoteViews.setProgressBar(R.id.progressBar1, 100, 0, false);
notification.contentView=remoteViews;
Intent intent1=new Intent(this, MainActivity.class);
notification.contentIntent=PendingIntent.getActivity(this, 0, intent1, 0);
manager.notify(R.drawable.ic_launcher, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
String url = intent.getStringExtra("url");
getAPKname(url);
new DowmAPK().execute(url);
return super.onStartCommand(intent, flags, startId);
}
private void getAPKname(String url)
{
int lastIndexOf = url.lastIndexOf('/');
apkName = url.substring(lastIndexOf+1);
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
class DowmAPK extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params)
{
InputStream is =null;
FileOutputStream fos=null;
try
{
URL url = new URL(params[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
if(con.getResponseCode()!=HttpURLConnection.HTTP_OK)
{
Log.e("HttpURLConnection", "连接异常");
return null;
}
int contentLength = con.getContentLength();
is = con.getInputStream();
fos = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+apkName);
byte[] buffer=new byte[1024];
int len=0;
int len2=0;
StringBuilder sb = new StringBuilder();
while(-1!=(len=is.read(buffer)))
{
len2+=len;
publishProgress(len2,contentLength);
fos.write(buffer, 0, len);
}
return "down ok!!!";
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(is!=null)
try
{
is.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(fos!=null)
try
{
fos.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values)
{
Log.e("onProgressUpdate", "onProgressUpdate");
//values[1] 总字节 values[0]已经读了的字节
remoteViews.setProgressBar(R.id.progressBar1, values[1], values[0], false);
Log.e("onProgressUpdate22", "onProgressUpdate22");
// Post a notification to be shown in the status bar.
//If a notification with the same id has already been posted by your application
//and has not yet been canceled,
//it will be replaced by the updated information.
//更新notification的内容,一定要通知,就是调下面的方法,不然进度条不会更新的
manager.notify(R.drawable.ic_launcher, notification);
}
@Override
protected void onPostExecute(String result)
{
if(result!=null)
{
notification.flags=Notification.FLAG_AUTO_CANCEL;
manager.notify(R.drawable.ic_launcher, notification);
Toast.makeText(getApplication(), "下载完毕", Toast.LENGTH_LONG).show();
String fileName = Environment.getExternalStorageDirectory() +"/"+apkName;
//启动系统的activity
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)),"application/vnd.android.package-archive");
//不是在activity里 要加这句话
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
}
发表评论
-
layout_weight的使用陷阱
2015-06-06 12:16 512<?xml version="1.0&quo ... -
PopupWindow的用法
2015-02-12 22:57 587package com.example.androidtes ... -
android studio最常用的快捷键
2014-03-18 18:05 919Shift+F6 重构-重命名 ... -
ACTION
2014-03-10 09:15 465@Override public void onClick( ... -
插入排序&&冒泡排序
2014-02-25 16:38 577public class test { public st ... -
将毫秒数转化成时间格式
2014-02-21 15:47 949protected CharSequence getTim ... -
ExpandableListView 中嵌套 gridview
2013-03-02 22:29 2318ExpandableListView 中嵌套 ... -
socket
2013-02-24 16:08 699//先启动服务器,设置监听的端口 package tes ... -
将两数组合并后排序
2013-01-26 15:15 905package com.wuxifu.bishi; im ... -
Android加载大图片内存溢出的问题总结
2013-01-24 01:08 638尽量不要使用setImageBitmap或setImageR ... -
借鉴 Mini音乐播放器
2013-01-22 23:04 572继上篇音乐播放器,我们讲到使用 SDCard 播放音乐的例 ... -
music player
2013-01-22 22:58 628播放器组件 fragment package com ... -
PopUpWindow
2013-01-19 15:27 1156private void iniPopUpWindow() ...
相关推荐
在众多版本中,“Windows 10 Upgrade 9252”是一个重要的里程碑,它在Windows 10的发展历程中占据着特殊的地位。这一版本不仅引入了多项新功能,还修复了前一版本存在的问题,旨在为用户提供更加稳定和流畅的操作...
"Linux刷机工具upgrade_tool"是Rockchip为这些设备提供的一个专用软件,用于更新或升级设备的操作系统。这个工具是Linux系统下的,意味着它专门设计在Linux环境下运行,帮助用户方便地对基于Rockchip处理器的设备...
在本文中,我们将深入探讨如何使用`upgrade_tool`来更新Nanopi-Neo4开发板上的软件,特别是在基于Android 8.1的系统环境中。Nanopi-Neo4是一款流行的开源硬件开发平台,广泛用于物联网(IoT)项目和嵌入式系统开发。...
本文将详细解析“长虹整机升级文件upgrade_ZLM65HiS2_MT5520_V1.00019.pkg”这一固件更新包,帮助用户理解升级过程及其对电视性能的影响。 首先,标题中的“upgrade_ZLM65HiS2_MT5520_V1.00019.pkg”揭示了该升级...
《Xhorse Upgrade Kit Software 固件更新工具详解》 Xhorse Upgrade Kit Software 是一款专为Xhorse品牌的各种工具设计的固件更新软件,主要用于提升这些工具的性能和修复潜在问题。这款工具覆盖了Xhorse品牌旗下的...
《VBUC.7.0 Visual Basic Upgrade Companion:VB代码向vb.net的自动化迁移之路》 在软件开发领域,随着技术的不断进步,编程语言也在持续更新。Visual Basic(VB)作为早期广泛使用的编程语言,随着时间的推移,...
Cisco Aironet AP to LWAPP Upgrade Tool v34是一款专为Cisco路由器、交换机、接入点(Access Point, AP)以及无线局域网控制器(Wireless LAN Controller, WLC)设计的软件升级工具。此工具的主要功能是确保网络...
ST-Link Upgrade Firmware V2.J33.S7.zip是一个用于更新ST-Link设备固件的压缩包文件。这个文件主要用于提升ST-Link调试器的性能和功能,使其能更好地配合STM8和STM32微控制器进行开发工作。ST-Link是意法半导体...
《Cisco网络设备升级工具——Aironet AP to LWAPP Upgrade Tool详解》 在现代网络环境中,Cisco设备,如路由器、交换机、接入点(AP)和无线局域网控制器(WLC),扮演着至关重要的角色。为了确保网络的稳定性和...
ST-Link Upgrade Firmware V2.J27.S6是STMicroelectronics为ST-Link调试器/编程器提供的固件更新程序,旨在提升其功能和性能。ST-Link是STMicroelectronics开发的一种工具,广泛用于STM8和STM32微控制器系列的调试和...
"华为T2010_升级工具(Upgrade_tool).zip" 是一个包含用于更新T2010设备软件的压缩包,它的重要性在于确保设备能够保持最新的功能和安全性。 升级工具通常包含以下几个关键部分: 1. **升级程序**:这是执行设备...
在某些情况下,当 Nginx 配置不当,尤其是 `Connection` 字段设置为 `Upgrade` 时,可能会导致 Kestrel(ASP.NET Core 的内置 web 服务器)返回 400 错误。这个问题通常与 WebSocket 协议升级有关。 WebSocket 是一...
本文将深入探讨如何通过"upgrade_ssh.zip"这个压缩包来升级CentOS系统的OpenSSL,以及与之相关的openssl-fisp和OpenSSH。 首先,OpenSSL是一个强大的安全工具包,它包含了各种加密算法、证书处理功能以及用于实现...
标题“SPD_Upgrade_Tool_R25.20.3901.zip”指的是 Spreadtrum Communication 公司的固件升级工具的一个版本,R25.20.3901 是该工具的特定修订号。Spreadtrum 是一家专注于移动通信芯片组的中国公司,他们的产品广泛...
Windows10Upgrade9252.exe
这个名为“中兴B860AV2.1-T固件包 a ml_upgrade_package.rar”的文件,实际上是一个包含了升级固件的压缩文件,它允许用户自行对设备进行系统升级。 首先,我们来理解一下“固件”(Firmware)的概念。固件是介于...
而今天介绍的 Windows11Upgrade 应用能够帮助你轻松绕过这些限制,不仅能帮你快速下载 Windows 11 系统镜像,而且能帮你安装升级。 在升级过程中你可以选择是否要执行升级并保存应用程序和数据,只保存数据而不保存...
《构建升级脚本的艺术——深入理解`make_upgrade_script`》 在IT行业中,软件更新与维护是不可或缺的一环,而有效的升级脚本则是确保这一过程顺利进行的关键工具。`make_upgrade_script`,这个看似简单的名字背后,...
ST-Link Upgrade Firmware V2.J30.S7 是STMicroelectronics(意法半导体)为ST-LINK系列编程器固件提供的一个更新版本。ST-LINK是STM8和STM32微控制器开发板上的一个标准调试和编程接口,它使得开发者能够方便地对...
ST-Link Upgrade Firmware V2.J34.S7.zip是一个针对ST-Link调试工具的固件升级包,主要用于更新ST-Link/V1开发板的固件版本。ST-Link是意法半导体(STMicroelectronics)推出的一种微控制器调试器和编程器,它允许...