A total of one class is used:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/57/41/wKiom1SWa3_jppJIAAB2XuKLomY692.jpg "title=" Main.png "alt=" wkiom1swa3_jppjiaab2xuklomy692.jpg "/> 650" this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/ 57/41/wkiom1swa-rjruh2aaejk-uvf2c912.jpg "title=" code.jpg "alt=" Wkiom1swa-rjruh2aaejk-uvf2c912.jpg "/>
Assuming that the current version is 1th, you receive the update notification for version 2nd through requests to the background. Code such as:
Package com.example.ex_templete;
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 java.net.URLConnection;
Import Android.net.Uri;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import android.app.Notification;
Import Android.app.NotificationManager;
Import android.app.PendingIntent;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.content.pm.PackageInfo;
Import Android.content.pm.PackageManager;
Import android.content.pm.PackageManager.NameNotFoundException;
Import Android.support.v4.app.NotificationCompat;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.RemoteViews;
public class Mainactivity extends Activity {
private static final String Path_apk_upgroup = Environment.getexternalstoragedirectory ()
+ "/myapp/download/myapp.apk";
private static final int notify_id = 1234;
Private Remoteviews remoteviews;
Private Notification Notification;
Private Notificationmanager Manager;
private int fileLen;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
AutoUpgrade ();
}
private void AutoUpgrade () {
//Get the current app version number
int VC = Getversioncode ();
/* Usually each time you open the app will request a version number in the background, if it is the same as the current version, do not remind, if larger than the current version, prompt upgrade */
int NEWVC = 2;//assumes that the version obtained from the background is version 2nd, greater than the current version
Final String Apkurl = "http://meishipic.qiniudn.com/2010052615045178.jpg";//apk, take a picture instead
String apkmessage = "1, added xxx \n2, modified xxx"; New features added
if (VC < NEWVC)
{
New Alertdialog.builder (this). Settitle ("Update Upgrade")
. Setmessage (Apkmessage)
. Setpositivebutton ("Upgrade", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Upgrade
New MyTask (). Execute (apkurl);
}
}). Setnegativebutton ("Cancel", null). Create (). Show ();
}
}
Class MyTask extends Asynctask<string, Integer, boolean>
{
@Override
protected void OnPreExecute () {
Super.onpreexecute ();
//notification
Shownotifi ();
}
@Override
Protected Boolean doinbackground (String ... params) {
Download apk
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL (params[0]);
HttpURLConnection openconnection = (httpurlconnection) url.openconnection ();
FileLen = Openconnection.getcontentlength ();//Get file length
int responsecode = Openconnection.getresponsecode ();
if (responsecode! = HTTPURLCONNECTION.HTTP_OK)
{
Tips
return null;
}
Determine if a file path exists
File file =new file (path_apk_upgroup);
if (!file.getparentfile (). exists ())
{
File.getparentfile (). Mkdirs ();
}
is = Openconnection.getinputstream ();
int len = 0;
byte[] buffer = new byte[1024];
Downloaded size
int loadlen = 0;
FOS = new FileOutputStream (path_apk_upgroup);
int num = 1;
while ( -1! = (len = is.read (buffer)))
{
thread.sleep (100);
Fos.write (buffer, 0, Len);
Loadlen + = Len;
if (Loadlen *100/filelen >= 1*num)
{
num++;
Publishprogress (Loadlen);
}
}
Fos.flush ();
} 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) {
Super.onprogressupdate (values);
Update notifications
int len = values[0];
Notification.contentView.setTextViewText (R.ID.TV12, "updated" + Len*100/filelen + "%");
Notification.contentView.setProgressBar (R.id.progressbar1, FileLen, Len, false);
Manager.notify (notify_id, notification);
}
@Override
protected void OnPostExecute (Boolean result) {
Super.onpostexecute (result);
Perfect Notice
Setting up a click notification installation
Intent Intent = new Intent (Intent.
Action_view);
Intent.setdataandtype (Uri.parse ("file://" +
Path_apk_upgroup),
"Application/vnd.android."
+ "package-archive");
Notification.contentView.setTextViewText (R.ID.TV12, "Update complete, click Install");
Notification.contentView.setViewVisibility (R.id.progressbar1, View.gone);
Pendingintent IC = pendingintent.getactivity (mainactivity.this, 0, intent, 0);
notification.contentintent = IC;
Manager.notify (notify_id, notification);
startactivity (intent);
}
}
private int Getversioncode () {
Packagemanager manager = Getpackagemanager ();
try {
PackageInfo PackageInfo =
Manager.getpackageinfo (Getpackagename (), 0);
return packageinfo.versioncode;
} catch (Namenotfoundexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return 0;
}
public void Shownotifi () {
Manager = (Notificationmanager) getsystemservice (Context.notification_service);
Intent Intent = new Intent ();
Intent.setclass (this, mainactivity.class);//Click Settings to jump to the page
Pendingintent contentintent = pendingintent.getactivity (this, 0,
Intent, 0);
Remoteviews = new
Remoteviews (Getpackagename (), r.layout.notification);
notification =
New Notificationcompat.builder (This)
. Setsmallicon (R.drawable.ic_launcher). Setcontenttitle ("notice")
. Setticker ("Heading 1"). SetContent (Remoteviews). Setcontenttext ("Content 1")
. Setwhen (System.currenttimemillis ())
. Setcontentintent (Contentintent).
Build ();
Manager.notify (notify_id, notification);
}
}
This enables the app to check the version prompt for updates. Don't miss the privilege.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/41/wKiom1SWbaHyhCg4AAHAZtrXSZg957.jpg "style=" float: none; "title=" simulator 1.jpg "alt=" Wkiom1swbahyhcg4aahaztrxszg957.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/3E/wKioL1SWbkrgCAweAAGOYEONcAg517.jpg "style=" float: none; "title=" simulator 2.jpg "alt=" Wkiol1swbkrgcaweaagoyeoncag517.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/41/wKiom1SWbaXT_2VVAAGUmmbQPQM478.jpg "style=" float: none; "title=" Simulator 3.jpg "alt=" Wkiom1swbaxt_2vvaagummbqpqm478.jpg "/>
This article is from "broken Childe's pseudo technology blog" blog, please be sure to keep this source http://pogongzi.blog.51cto.com/8982426/1592282
Android App Upgrade module parsing