- 浏览: 35848 次
- 性别:
- 来自: 厦门
文章分类
最新评论
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 468<?xml version="1.0&quo ... -
PopupWindow的用法
2015-02-12 22:57 545package com.example.androidtes ... -
android studio最常用的快捷键
2014-03-18 18:05 875Shift+F6 重构-重命名 ... -
ACTION
2014-03-10 09:15 393@Override public void onClick( ... -
插入排序&&冒泡排序
2014-02-25 16:38 540public class test { public st ... -
将毫秒数转化成时间格式
2014-02-21 15:47 901protected CharSequence getTim ... -
ExpandableListView 中嵌套 gridview
2013-03-02 22:29 2282ExpandableListView 中嵌套 ... -
socket
2013-02-24 16:08 665//先启动服务器,设置监听的端口 package tes ... -
将两数组合并后排序
2013-01-26 15:15 870package com.wuxifu.bishi; im ... -
Android加载大图片内存溢出的问题总结
2013-01-24 01:08 608尽量不要使用setImageBitmap或setImageR ... -
借鉴 Mini音乐播放器
2013-01-22 23:04 538继上篇音乐播放器,我们讲到使用 SDCard 播放音乐的例 ... -
music player
2013-01-22 22:58 596播放器组件 fragment package com ... -
PopUpWindow
2013-01-19 15:27 1117private void iniPopUpWindow() ...
相关推荐
Windows10Upgrade9252.zip
而今天介绍的 Windows11Upgrade 应用能够帮助你轻松绕过这些限制,不仅能帮你快速下载 Windows 11 系统镜像,而且能帮你安装升级。 在升级过程中你可以选择是否要执行升级并保存应用程序和数据,只保存数据而不保存...
rockchip linux刷机工具upgrade_tool
-On ST-Link V2, upgrade firmware to V2.J27.S6, -需要其他版本请关注我
USB升级(前提:能开机):第一种:upgrade_loader.pkg ,(1)从本站上拷贝所需批次的升级软件解压后,如出现文件名为upgrade_loader.pkg 的升级软件拷贝到U 盘根目录下; 注意升级软件必须是PKG 文件(不是bin...
中兴B860AV2.1-T刷机固件包,下载固件 a ml_upgrade_package.img 本人花钱购买学习测试有效
Visual Basic Upgrade Companion
Running the Pre-Upgrade Checker . . . . 6 Step 3. Migration to vCenter Server 4.1 . . . . . . 13 Step 4. Upgrading hosts to ESX(i) 4.1 . . . . . . . . 33 Step 5. Using vihostupdate to upgrade ESXi ...
目前centos6已经停止更新,rehdat提供了centos6升级至centos7的工具,一般网络下载速度感人,此资源包含升级所需的preupgrade-assistant、preupgrade-assistant-contents、preupgrade-assistant-ui、redhat-upgrade-...
Official Oracle upgrade 11g R2 upgrade guide
RK27Upgrade_v2.6.4
RK27Upgrade_v2.5(Single)
Windows 7 Upgrade Advisor Windwos 7 升级检测建议
maomi-3-1-2-release-upgrade.apk
ST-LINK, ST-LINK/V2, ST-LINK/V2-1 firmware upgrade
安装openssl-fisp,openssl,openssh运行upgrade_ssh.zip支持在线更新多种版本centos的OpenSSL的shell程序解读
Windows 7and8 Enterprise Upgrade 注册码
浏览器Upgrade-Insecure-Requests详解 搭建HTTPS服务器时经常会遇到该请求头
mysql_upgrade -u root -p
F650A_SIP_V2.30.10P2T2_UPGRADE_BOOTLDR.bin