Home > Mobile >  Android studio how to do a download the function of the app
Android studio how to do a download the function of the app

Time:09-20

If there is any case or framework, trouble, thank you

CodePudding user response:

Do you have an apk download address, and then grab a download file to the code, volley, okhttp, retrofit, etc

CodePudding user response:

Basically has download resources of network library classes, as to download the app file download, download style, download after installation, each version of the installation of adaptation, is your own code.

CodePudding user response:

 
Public class DownloadApk {

Private volatile static DownloadApk mInstance=null;
Private Context mContext;

Private DownloadApk () {

}

Public static DownloadApk getInstance () {
If (mInstance==null) {
Synchronized (DownloadApk. Class) {
If (mInstance==null) {
MInstance=new DownloadApk ();
}
}
}
Return mInstance;
}

Public void downLoadApk (final Context mContext, final String url) {
Enclosing mContext=mContext;
//the progress bar
MProgressDialog=new Dialog (mContext, R.s tyle. FullHeightDialog);
MProgressDialog. The setContentView (R.l ayout. Loading_dialog);
MProgressDialog. SetCanceledOnTouchOutside (false);
MProgress=mProgressDialog. The findViewById (R.i d.p rogressBar);
MTvProgressCount=mProgressDialog. The findViewById (R.i which v_progress_count);
ImageView ivDismiss=mProgressDialog. The findViewById (R.i d.i v_dismiss);
IvDismiss. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
//Toast. MakeText (mContext, "silent download... ", Toast. LENGTH_SHORT). The show ();
ProgressDismiss ();
}
});
MProgress. SetMax (100);
MProgressDialog. The show ();
New Thread () {
@ Override
Public void the run () {
Try {
Urls URL=new URL (URL);
HttpURLConnection conn=(HttpURLConnection) urls. OpenConnection ();
Conn. SetConnectTimeout (5000);
Conn. SetUseCaches (false);//do not use the cache
Conn. The connect ();
Int responseCode=conn. GetResponseCode ();
If (responseCode==HttpURLConnection. HTTP_OK) {
//access to the file size
Int contentLength=conn. GetContentLength ();
InputStream is=conn. GetInputStream ();
File File=new File (Environment. External.getexternalstoragedirectory () + "/book. Apk");
FileOutputStream fos=new FileOutputStream (file);
BufferedInputStream bis=new BufferedInputStream (is);
Byte [] buffer=new byte [1024].
Int len.
Int total=0;
While ((len=bis. Read (buffer))!=1) {
Fos. Write (buffer, 0, len);
Total +=len;
If (System. CurrentTimeMillis () - curTime & gt; 1000 | | total=={contentLength)
//get the current downloads
Long progress=total * 100 l/contentLength;
//the UI in the main thread, directly installed to abnormal
MProgress. SetProgress (progress) (int);
The Message the Message=new Message ();
Message. What=0;
Message. Arg1=(int) progress;
Handler. SendMessage (message);
CurTime=System. CurrentTimeMillis ();
}
}
fos.close();
The bis. Close ();
Is the close ();
//install APK
InstallApk (file, mContext);
ProgressDismiss ();//end of the progress bar dialog
}
The else {
//ToastUtil (mContext, "application installer does not exist");
The Message the Message=new Message ();
Message. What=1;
Bundle Bundle=new Bundle ();
Bundle. PutString (" Error ", "application installer does not exist");
Message. SetData (bundle);
Handler. SendMessage (message);
}
} the catch (Exception e) {
e.printStackTrace();
}
}
}. The start ();
}

Private void progressDismiss () {
If (mProgressDialog!=null & amp; & MProgressDialog. IsShowing ()) {
MProgressDialog. Dismiss ();
MProgressDialog=null;
}
}

Private Dialog mProgressDialog;
Private ProgressBar mProgress;
Private TextView mTvProgressCount;
Private long curTime=0;

Public void installApk (File File, the Context mContext) {
Try {
Intent Intent=new Intent ();
Intent. SetAction (intent. ACTION_VIEW);
If (Build) VERSION) SDK_INT & gt;=Build. VERSION_CODES. N) {
//Android7.0 and above version
Intent. SetFlags (intent. FLAG_ACTIVITY_NEW_TASK);
Intent. AddFlags (intent. FLAG_GRANT_READ_URI_PERMISSION | intent. FLAG_GRANT_WRITE_URI_PERMISSION);
//(mContext, "application package name" + ". FileProvider ", file)
Uri contentUri=FileProvider. GetUriForFile (mContext, mContext getApplicationContext () getPackageName () + ". FileProvider ", the file).
Intent. SetDataAndType (contentUri, "application/VND. Android. Package - archive");

} else {
//below Android7.0 version
Intent. SetDataAndType (Uri) fromFile (file), "application/VND. Android. Package - archive");
Intent. SetFlags (intent. FLAG_ACTIVITY_NEW_TASK);
Intent. AddFlags (intent. FLAG_GRANT_READ_URI_PERMISSION | intent. FLAG_GRANT_WRITE_URI_PERMISSION);
}
MContext. StartActivity (intent);
} the catch (Exception e) {
e.printStackTrace();
//progressDismiss ();
}

}

Private Handler Handler=new Handler () {
@ Override
Public void handleMessage (Message MSG) {
Super. HandleMessage (MSG);
The switch (MSG. What) {
Case 0:
Int progress=MSG. Arg1;
MTvProgressCount. SetText (progress + "%"); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related