Android program version Update-notification bar update download and installation, android program version

Source: Internet
Author: User

Android program version Update-notification bar update download and installation, android program version

After the Android app checks for version updates, download the updated version in the notification bar, update the download progress, and automatically install the downloaded version as follows:

  • Check the current version number

VersionCode in the AndroidManifest file is used to identify the version, and a new version of apk is put on the server. versioncode is later than the current version. The following code is used to obtain the versioncode value.

PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);int localVersion = packageInfo.versionCode;

Compare the current versioncode with the server. If the value is smaller than, update the version.

  • Download the apk File
/*** Download apk ** @ param apkUri */
Private void downLoadNewApk (String apkUri, String version) {manager = (icationicationmanager) context. getSystemService (context. NOTIFICATION_SERVICE); y = new Notification (); notify. icon = R. drawable. ic_launcher; // The layout file notify is displayed in the notification bar. contentView = new RemoteViews (context. getPackageName (), R. layout. view_policy_item); manager. Y (100, notify); // create the downloaded apk File fileInstall = FileOperate. mkdirSdcardFile ("downLoad", APK_NAME + version + ". apk "); downLoadSchedule (apkUri, completeHandler, context, fileInstall );}
FileOperate is a self-written file tool class.

Layout shown in the notification bar, view_policy_item.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginLeft="10dp"    android:background="#00000000"    android:padding="5dp" >    <ImageView        android:id="@+id/notify_icon_iv"        android:layout_width="25dp"        android:layout_height="25dp"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/notify_updata_values_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginBottom="6dp"        android:layout_marginLeft="15dp"        android:layout_marginTop="5dp"        android:layout_toRightOf="@id/notify_icon_iv"        android:gravity="center_vertical"        android:text="0%"        android:textColor="@color/white"        android:textSize="12sp" />    <ProgressBar        android:id="@+id/notify_updata_progress"        style="@android:style/Widget.ProgressBar.Horizontal"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/notify_icon_iv"        android:layout_marginTop="4dp"        android:max="100" /></RelativeLayout>
/*** Connect to the network, download a file, and return the progress ** @ param uri * @ param handler * @ param context * @ param file */
Public static void downLoadSchedule (final String uri, final Handler handler, Context context, final File file) {if (! File. exists () {handler. sendEmptyMessage (-1); return;} // The final int perLength = 4096; new Thread () {@ Override public void run () {super. run (); try {URL url = new URL (uri); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setDoInput (true); conn. connect (); InputStream in = conn. getInputStream (); // 2865412 long length = conn. getContentLength (); // read 1 k byte [] B each time Uffer = new byte [perLength]; int len =-1; FileOutputStream out = new FileOutputStream (file); int temp = 0; while (len = in. read (buffer ))! =-1) {// write the file out. write (buffer, 0, len); // current progress int schedule = (int) (file. length () * 100)/length); // notifies you of the update progress (10, 7, 4). if (temp! = Schedule & (schedule % 10 = 0 | schedule % 4 = 0 | schedule % 7 = 0 )) {// ensure that the same data is sent only once temp = schedule; handler. sendEmptyMessage (schedule) ;}} out. flush (); out. close (); in. close ();} catch (IOException e) {e. printStackTrace ();}}}. start ();}

Handler is updated based on the download progress

  • Update the progress bar
/***** Update notification bar */
Private Handler completeHandler = new Handler () {public void handleMessage (android. OS. message msg) {// update the notification bar if (msg. what <100) {policy. contentView. setTextViewText (R. id. notify_updata_values_ TV, msg. what + "%"); required y. contentView. setProgressBar (R. id. yy_updata_progress, 100, msg. what, false); manager. required y (100, required y);} else {required y. contentView. setTextViewText (R. id. notify_updata_values_ TV, "download completed"); y. contentView. setProgressBar (R. id. yy_updata_progress, 100, msg. what, false); // clear the notification bar manager. cancel (100); installApk (fileInstall );}};};

After the download is complete, call the system installation.

  • Install apk
/***** Install apk ** @ param file */
Private void installApk (File file) {Intent intent = new Intent (); intent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); intent. setAction (android. content. intent. ACTION_VIEW); intent. setDataAndType (Uri. fromFile (file), "application/vnd. android. package-archive "); context. startActivity (intent );}

Installation completed


Android can update the notification bar

The network of the computer used on the simulator,
The mobile phone is on the network of the mobile phone. How is the network speed of your mobile phone?
 
After Samsung 5830i is updated, the notification bar displays software updates, which is not caused by the installation of other software.

Open settings first-Application-samsnug apps open, click Close, then click the Software Update icon in the toolbar-click Install, and then the mobile phone will automatically restart. All I need is gt s5830i. this is done.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.