Android implementation program automatically upgrades to installation sample sharing (download Android installer package) _android

Source: Internet
Author: User
Tags stub

Copy Code code as follows:

Program Download Upgrade Zhouxiang
@JavascriptInterface
public void UPDATECAECP (final String path) {
try{
Alertdialog.builder Builder = new Builder (context) obj;
Builder.setmessage ("Detected a new release, whether to download upgrades?");
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 Upgrades");
M_pdialog.setmessage ("Downloading the latest version of CAECP, please wait ...");
M_pdialog.seticon (R.drawable.ic_launcher);
M_pdialog.setprogress (100);
M_pdialog.setindeterminate (FALSE);
Set whether ProgressDialog can be canceled by pressing the return key
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");
}
}

Copy Code code as follows:

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 new thread, call system to install
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 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.