Android programmatically update application when a new version is available

If you have apps that are hosted on your server and as time passes you make enhancements, fix bugs, etc. then comes the question how can I alert all my app users that a newer version is out there and make it real easy for them to download. You can email all your users with the link to the new version, that will be very cumbersome. Well that process needs to be built right into the app you deploy it for the first time.
Here is an example how to do it, the basic idea is outlined below
  1. Create a Web Service which your app can poll whenever the app launches or based on some time limit that can check if there is new version out there.
  2. This Web service should return the lastest Version of the apk file that is hosted on the Server along with the URI of the application file that has the new version.
  3. When your app gets the response from the Web Service, it will parse the JSON and check your app version to the lastest version that is available on the server.
  4. If your app version is lower than the latest version it will prompt the user to start the download process.
  5. The download of the new app is handled by the Download Manager.
  6. The download manager will notify your app using Broadcast receiver when the download is complete.
  7. Upon completion of the latest version of the application file the you can start the activity to install that file.
  8. At this point user needs to say OKAY, lets do it.
Android update application programmatically

Java Servlet Web Service to check New Version - CheckAppVersion.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.as400samplecode;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.google.gson.JsonObject;
 
public class CheckAppVersion extends HttpServlet {
 private static final long serialVersionUID = 1L;
 
 public CheckAppVersion() {
  super();
 }
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  doPost(request,response);
 }
 
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 
  PrintWriter out = response.getWriter();
  response.setContentType("text/html");
   
  //send a JSON response with the app Version and file URI
  JsonObject myObj = new JsonObject();
  myObj.addProperty("success", true);
  myObj.addProperty("latestVersion", 2);
  out.println(myObj.toString());
  out.close();
 
 
 }
  
}

Application Manifest file - AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
    package="com.as400samplecode"
    android:versionCode="1"
    android:versionName="2.1.3" >
 
    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="17" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.as400samplecode.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Application Layout file - activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 android:layout_height="match_parent" tools:context=".MainActivity"
 android:padding="5dp">
 
 <TextView android:id="@+id/versionName" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true" android:layout_marginTop="16dp"
  android:text="What Version?" android:textAppearance="?android:attr/textAppearanceMedium"
  android:textStyle="bold" />
 
 <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:layout_below="@+id/versionName"
  android:layout_marginTop="16dp" android:text="@string/copyright"
  android:textAppearance="?android:attr/textAppearanceMedium" />
 
</RelativeLayout>

IntentService for making Web Service requests - MyWebService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.as400samplecode;
  
import java.io.ByteArrayOutputStream;
import java.io.IOException;
  
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
 
import com.as400samplecode.MainActivity.MyWebReceiver;
  
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
  
public class MyWebService  extends IntentService{
  
 private static final String LOG_TAG = "MyWebService";
    public static final String REQUEST_STRING = "myRequest";
    public static final String RESPONSE_STRING = "myResponse";
    public static final String RESPONSE_MESSAGE = "myResponseMessage";
  
    private String URL = null;
    private static final int REGISTRATION_TIMEOUT = 3 * 1000;
    private static final int WAIT_TIMEOUT = 30 * 1000;
  
    public MyWebService() {
        super("MyWebService");
    }
  
    @Override
    protected void onHandleIntent(Intent intent) {
  
        String requestString = intent.getStringExtra(REQUEST_STRING);
        Log.v(LOG_TAG, requestString);
        String responseMessage = "";
         
        try {
  
            URL = requestString;
            HttpClient httpclient = new DefaultHttpClient();
            HttpParams params = httpclient.getParams();
  
            HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
            ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);
  
            HttpGet httpGet = new HttpGet(URL);
            HttpResponse response = httpclient.execute(httpGet);
  
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseMessage = out.toString();
            }
  
            else{
                Log.w("HTTP1:",statusLine.getReasonPhrase());
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
  
        } catch (ClientProtocolException e) {
            Log.w("HTTP2:",e );
            responseMessage = e.getMessage();
        } catch (IOException e) {
            Log.w("HTTP3:",e );
            responseMessage = e.getMessage();
        }catch (Exception e) {
            Log.w("HTTP4:",e );
            responseMessage = e.getMessage();
        }
  
  
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction(MyWebReceiver.PROCESS_RESPONSE);
        broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
        broadcastIntent.putExtra(RESPONSE_MESSAGE, responseMessage);
        sendBroadcast(broadcastIntent);
  
    }
  
}

Application Activity - MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.as400samplecode;
 
import org.json.JSONException;
import org.json.JSONObject;
 
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
 private static final String LOG_TAG = "AppUpgrade";
 private MyWebReceiver receiver;
 private int versionCode = 0;
 String appURI = "";
 
 private DownloadManager downloadManager;
 private long downloadReference;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  //Overall information about the contents of a package
  //This corresponds to all of the information collected from AndroidManifest.xml.
  PackageInfo pInfo = null;
  try {
   pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
  }
  catch (NameNotFoundException e) {
   e.printStackTrace();
  }
  //get the app version Name for display
  String version = pInfo.versionName;
  //get the app version Code for checking
  versionCode = pInfo.versionCode;
  //display the current version in a TextView
  TextView versionText = (TextView) findViewById(R.id.versionName);
  versionText.setText(version);
 
  //Broadcast receiver for our Web Request
  IntentFilter filter = new IntentFilter(MyWebReceiver.PROCESS_RESPONSE);
  filter.addCategory(Intent.CATEGORY_DEFAULT);
  receiver = new MyWebReceiver();
  registerReceiver(receiver, filter);
 
  //Broadcast receiver for the download manager
  filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
  registerReceiver(downloadReceiver, filter);
 
  //check of internet is available before making a web service request
  if(isNetworkAvailable(this)){
   Intent msgIntent = new Intent(this, MyWebService.class);
   msgIntent.putExtra(MyWebService.REQUEST_STRING, "http://demo.mysamplecode.com/Servlets_JSP/CheckAppVersion");
   startService(msgIntent);
  }
 
 }
 
 @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;
 }
 
 @Override
 public void onDestroy() {
  //unregister your receivers
  this.unregisterReceiver(receiver);
  this.unregisterReceiver(downloadReceiver);
  super.onDestroy();
 }
 
 //check for internet connection
 private boolean isNetworkAvailable(Context context) {
  ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  if (connectivity != null) {
   NetworkInfo[] info = connectivity.getAllNetworkInfo();
   if (info != null) {
    for (int i = 0; i < info.length; i++) {
     Log.v(LOG_TAG,String.valueOf(i));
     if (info[i].getState() == NetworkInfo.State.CONNECTED) {
      Log.v(LOG_TAG, "connected!");
      return true;
     }
    }
   }
  }
  return false;
 }
 
 //broadcast receiver to get notification when the web request finishes
 public class MyWebReceiver extends BroadcastReceiver{
 
  public static final String PROCESS_RESPONSE = "com.as400samplecode.intent.action.PROCESS_RESPONSE";
 
  @Override
  public void onReceive(Context context, Intent intent) {
 
   String reponseMessage = intent.getStringExtra(MyWebService.RESPONSE_MESSAGE);
   Log.v(LOG_TAG, reponseMessage);
 
   //parse the JSON response
   JSONObject responseObj;
   try {
    responseObj = new JSONObject(reponseMessage);
    boolean success = responseObj.getBoolean("success");
    //if the reponse was successful check further
    if(success){
     //get the latest version from the JSON string
     int latestVersion = responseObj.getInt("latestVersion");
     //get the lastest application URI from the JSON string
     appURI = responseObj.getString("appURI");
     //check if we need to upgrade?
     if(latestVersion > versionCode){
      //oh yeah we do need an upgrade, let the user know send an alert message
      AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
      builder.setMessage("There is newer version of this application available, click OK to upgrade now?")
      .setPositiveButton("OK", new DialogInterface.OnClickListener() {
       //if the user agrees to upgrade
       public void onClick(DialogInterface dialog, int id) {
        //start downloading the file using the download manager
        downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
        Uri Download_Uri = Uri.parse(appURI);
        DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        request.setAllowedOverRoaming(false);
        request.setTitle("My Andorid App Download");
        request.setDestinationInExternalFilesDir(MainActivity.this,Environment.DIRECTORY_DOWNLOADS,"MyAndroidApp.apk");
        downloadReference = downloadManager.enqueue(request);
       }
      })
      .setNegativeButton("Remind Later", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
        // User cancelled the dialog
       }
      });
      //show the alert message
      builder.create().show();
     }
 
    }
   } catch (JSONException e) {
    e.printStackTrace();
   }
 
  }
 
 }
 
 //broadcast receiver to get notification about ongoing downloads
 private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
 
  @Override
  public void onReceive(Context context, Intent intent) {
 
   //check if the broadcast message is for our Enqueued download
   long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
   if(downloadReference == referenceId){
 
    Log.v(LOG_TAG, "Downloading of the new app version complete");
    //start the installation of the latest version
    Intent installIntent = new Intent(Intent.ACTION_VIEW);
    installIntent.setDataAndType(downloadManager.getUriForDownloadedFile(downloadReference),
        "application/vnd.android.package-archive");
    installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(installIntent);
     
   }
  }
 };
 
}

Reference

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.