android implementation automatically upgrades to the installation sample share of download the android program installation package

  • 2020-05-26 10:04:27
  • OfStack


// Program download and upgrade  zhouxiang
@JavascriptInterface
public void UpdateCAECP(final String path){
try{
AlertDialog.Builder builder = new Builder((Context)obj);
builder.setMessage( "A new release has been detected , Whether to download an upgrade ? " );
builder.setTitle(" Program update prompt ");
builder.setPositiveButton(" upgrade ", new OnClickListener(){
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
m_pDialog = new ProgressDialog((Context)obj);
m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
m_pDialog.setTitle(" Program upgrade in progress ");
m_pDialog.setMessage(" Downloading the latest version CAECP, Please wait... ");
m_pDialog.setIcon(R.drawable.ic_launcher);
m_pDialog.setProgress(100);
m_pDialog.setIndeterminate(false);
// Set up the ProgressDialog  Can you press the back button to cancel 
m_pDialog.setCancelable(true);
m_pDialog.show();
new CAECP_DownloadFile(m_pDialog,(Context)obj).execute(path);
}
});
builder.setNegativeButton(" cancel ", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();
}catch(Exception e){
Alert(" Upgrade prompt ", e.getMessage(), " confirm ");
}
}


//zhouxiang  File download percentage   and   Automatic installation 
public class CAECP_DownloadFile extends AsyncTask{
ProgressDialog m_pDialog=null;
String path="/sdcard/caecp/caecp.apk";
static String chattemp = "/sdcard/caecp/chat.caecp";
static String usertemp = "/sdcard/caecp/user.caecp";
Context obj;
CAECP_DownloadFile(ProgressDialog m_pDialog2,Context obj2){
m_pDialog=m_pDialog2;
obj=obj2;
}
@Override
protected String doInBackground(String ...  sUrl) {
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
int fileLength = connection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(path);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
m_pDialog.setProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
DownCAECP_Ok();
} catch (Exception e) {
}
return null;
}
// download CAECP File complete , Start a new thread , Call the system for installation 
public void DownCAECP_Ok(){
new Thread(){
public void run() {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setDataAndType(Uri.parse("file://" + path),"application/vnd.android.package-archive");
obj.startActivity(i);
}
}.start();
}


Related articles: