1. 程式人生 > >使用Rertrofit+Rxjava實現Android版本更新功能

使用Rertrofit+Rxjava實現Android版本更新功能

使用Retrofit+Rxjava實現版本更新的功能

效果圖

               

實現思路:

(一)從伺服器上下載資料,得到版本號,與手機中app的版本號進行對比,如果伺服器版本號大於當前應用的版本號,則提示,下載更新(因為無法對外提供伺服器資料,因此 本文只提供下載apk的方法,判斷是否更新的方法,根據伺服器傳遞的資料,自行載入判斷,獲取當前手機的版本號方法,會提供。)

(二)下載apk,並且監聽下載的進度,通過自定義的AlertDialog實現進度的展示

(三)下載完成,進行自定安裝

具體實現:

I、得到當前手機應用的版本號

我們可以在AndroidManifest.xml中設定程式的版本號等,如

Android:versionName="1.0",那如果想在程式碼中獲取這個版本號呢,可以用如下方法(這些修改版本號時只需要修改AndroidManifest.xml中的設定,不用修改程式碼了):

  1. /** 
  2.  * 返回當前程式版本名 
  3.  */  
  4. public static String getAppVersionName(Context context) {  
  5.     String versionName = "";  
  6.     try {  
  7.         // ---get the package info---  
  8.         PackageManager pm = context.getPackageManager();  
  9.         PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);  
  10.         versionName = pi.versionName;  
  11.         versioncode = pi.versionCode;
  12.         if (versionName == null || versionName.length() <= 0) {  
  13.             return "";  
  14.         }  
  15.     } catch (Exception e) {  
  16.         Log.e("VersionInfo"
    "Exception", e);  
  17.     }  
  18.     return versionName;  
  19. }
  20.  獲取當前應用的版本號:
  21. private String getVersionName() throws Exception  {  

     // 獲取packagemanager的例項

            PackageManager packageManager = getPackageManager();  

           // getPackageName()是你當前類的包名,0代表是獲取版本資訊  、

         PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(),0);  

            String version = packInfo.versionName;  

           return version;  }  



II、從伺服器得到版本號getServerVersion()。。。(由於保密原因該方法無法提供,比較簡單,自己可以根據自己專案的介面,下載得到,在此不再贅述)

III、自定義Dialog,用於顯示下載的進度條。

Android SDK已經提供有進度條元件ProgressDialog元件,但用的時候我們會發現可能風格與我們應用的整體風格不太搭配,而且ProgressDialog的可定製性也不太強,這時就需要我們自定義實現一個ProgressDialog。
通過看原始碼我們發現,ProgressDialog繼承自Alertdialog,有一個ProgressBar和兩個TextView組成的,通過對ProgressDialog的原始碼進行改進就可以實現一個自定義的ProgressDialog。

1、效果:

首先看一下自定義CommonProgressDialog和原生ProgressDialog的對比:

2、程式碼aa.xml 佈局檔案:

<FrameLayout 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">
    <LinearLayout
android:layout_width="wrap_content"
android:layout_height="460px"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical"
android:background="@mipmap/bgdialog">
        <TextView
android:id="@+id/progress_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="44px"
android:layout_marginTop="113px"
android:layout_gravity="center_horizontal"
android:textColor="#ffffff"
/>
        <ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="712px"
android:layout_height="30px"
android:layout_marginTop="100px"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:progressDrawable="@drawable/bb"
/>

        <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
            <TextView
android:id="@+id/progress_percent"
android:layout_width="80px"
android:layout_height="wrap_content"
android:textSize="30px"
android:layout_marginLeft="280px"
android:gravity="center_horizontal"
android:textColor="#ffffff"
/>
            <TextView
android:id="@+id/progress_number"
android:layout_width="250px"
android:layout_height="wrap_content"
android:layout_marginLeft="120px"
android:textSize="30px"
android:gravity="center_horizontal"
android:textColor="#ffffff"
/>
        </LinearLayout>
        <Button
android:id="@+id/btn_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消下載 "
/>
    </LinearLayout>
</FrameLayout>
2 、bb.xml Progressbar進度條圖片和背景圖片設定(主要設定了進度條的形狀(圓角),以及進度顯色的顏色)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">

              <shape>

                 <corners android:radius="5dip" />

                <gradient
android:endColor="#ddd"
android:startColor="#ddd" />
              </shape>
         </item>
      <item android:id="@android:id/progress">

           <clip>

               <shape>

                   <corners android:radius="5dip" />

                    <gradient
android:endColor="#ddd"
android:startColor="#80ff0000" />
              </shape>
          </clip>
      </item>

</layer-list>
3、CustomerDialog.java類:
package org.jokar.download_test.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import org.jokar.download_test.R;

import java.text.NumberFormat;
/**
 * Created by Administrator on 2016/12/8.
 */
public class CustomerDialog extends AlertDialog {
    private ProgressBar mProgress;
    private TextView mProgressNumber;
    private TextView mProgressPercent;
    private TextView mProgressMessage;
    private Handler mViewUpdateHandler;
    private int mMax;
    private CharSequence mMessage;
    private boolean mHasStarted;
    private int mProgressVal;
    private Button btn_cancle;

    private String TAG="CommonProgressDialog";
    private String mProgressNumberFormat;
    private NumberFormat mProgressPercentFormat;

    public void setBonClickListenre(BonClickListenre bonClickListenre) {
        this.bonClickListenre = bonClickListenre;
    }

    private BonClickListenre bonClickListenre;
    @RequiresApi(api = Build.VERSION_CODES.N)
    public CustomerDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
initFormats();
    }

    @Override
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
        setContentView(R.layout.aa);
        mProgress=(ProgressBar) findViewById(R.id.progress);
        mProgressNumber=(TextView) findViewById(R.id.progress_number);
        mProgressPercent=(TextView) findViewById(R.id.progress_percent);
        mProgressMessage=(TextView) findViewById(R.id.progress_message);
        btn_cancle= (Button) findViewById(R.id.btn_cancle);
        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View view) {
                bonClickListenre.BBonClick(view);
            }
        });

        //      LayoutInflater inflater = LayoutInflater.from(getContext());
mViewUpdateHandler = new Handler() {



            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
super.handleMessage(msg);
                int progress = mProgress.getProgress();
                int max = mProgress.getMax();
                double dProgress = (double)progress/(double)(1024 * 1024);
                double dMax = (double)max/(double)(1024 * 1024);
                if (mProgressNumberFormat != null) {
                    String format = mProgressNumberFormat;
                    mProgressNumber.setText(String.format(format, dProgress, dMax));
                } else {
                    mProgressNumber.setText("");
                }
                if (mProgressPercentFormat != null) {
                    double percent = (double) progress / (double) max;
                    SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent));
                    tmp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
                            0, tmp.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    mProgressPercent.setText(tmp);
                } else {
                    mProgressPercent.setText("");
                }
            }

        };
        if (mMessage != null) {
            setMessage(mMessage);
        }
        if (mMax > 0) {
            setMax(mMax);
        }
        if (mProgressVal > 0) {
            setProgress(mProgressVal);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initFormats() {
        mProgressNumberFormat = "%1.2fM/%2.2fM";
        mProgressPercentFormat = NumberFormat.getPercentInstance();
        mProgressPercentFormat.setMaximumFractionDigits(0);
    }
    private void onProgressChanged() {
        mViewUpdateHandler.sendEmptyMessage(0);


    }
    public void setProgressStyle(int style) {
        //mProgressStyle = style;
}
    public int getMax() {
        if (mProgress != null) {
            return mProgress.getMax();
        }
        return mMax;
    }
    public void setMax(int max) {
        if (mProgress != null) {
            mProgress.setMax(max);
            onProgressChanged();
        } else {
            mMax = max;
        }
    }
    public void setIndeterminate(boolean indeterminate) {
        if (mProgress != null) {
            mProgress.setIndeterminate(indeterminate);
        }
        //      else {
        //            mIndeterminate = indeterminate;
        //        }
}
    public void setProgress(int value) {
        if (mHasStarted) {
            mProgress.setProgress(value);
            onProgressChanged();
        } else {
            mProgressVal = value;
        }
    }


    @Override
public void setMessage(CharSequence message) {
        // TODO Auto-generated method stub
//super.setMessage(message);
if(mProgressMessage!=null){
            mProgressMessage.setText(message);
        }
        else{
            mMessage = message;
        }
    }


    public  interface BonClickListenre{
        public void BBonClick(View view);
    }
    @Override
protected void onStart() {
        // TODO Auto-generated method stub
super.onStart();
        mHasStarted = true;
    }


    @Override
protected void onStop() {
        // TODO Auto-generated method stub
super.onStop();
        mHasStarted = false;
    }
}
IV:下載apk,並且監聽進度(關鍵步驟)
(1):增加下載進度監聽
public class DownloadProgressInterceptor implements Interceptor {

    private DownloadProgressListener listener;

    public DownloadProgressInterceptor(DownloadProgressListener listener) {
        this.listener = listener;
    }

    @Override
public Response intercept(Chain chain) throws IOException {
        Response originalResponse = chain.proceed(chain.request());

        return originalResponse.newBuilder()
                .body(new DownloadProgressResponseBody(originalResponse.body(), listener))
                .build();
    }
}
ii:
/**
 * 下載進度listener
 * Created by JokAr on 16/5/11.
 */
public interface DownloadProgressListener {
    void update(long bytesRead, long contentLength, boolean done);
}
iii:
/**
 * ResponseBody for download
 * Created by JokAr on 16/5/11.
 */
public class DownloadProgressResponseBody extends ResponseBody {

    private ResponseBody responseBody;
    private DownloadProgressListener progressListener;
    private BufferedSource bufferedSource;

    public DownloadProgressResponseBody(ResponseBody responseBody,
                                        DownloadProgressListener progressListener) {
        this.responseBody = responseBody;
        this.progressListener = progressListener;
    }

    @Override
public MediaType contentType() {
        return responseBody.contentType();
    }

    @Override
public long contentLength() {
        return responseBody.contentLength();
    }

    @Override
public BufferedSource source() {
        if (bufferedSource == null) {
            bufferedSource = Okio.buffer(source(responseBody.source()));
        }
        return bufferedSource;
    }

    private Source source(Source source) {
        return new ForwardingSource(source) {
            long totalBytesRead = 0L;

            @Override
public long read(Buffer sink, long byteCount) throws IOException {
                long bytesRead = super.read(sink, byteCount);
                // read() returns the number of bytes read, or -1 if this source is exhausted.
totalBytesRead += bytesRead != -1 ? bytesRead : 0;

                if (null != progressListener) {
                    progressListener.update(totalBytesRead, responseBody.contentLength(), bytesRead == -1);
                }
                return bytesRead;
            }
        };

    }
}
(2) 建立下載進度的元素類:
/**
 * Created by JokAr on 16/7/5.
 */
public class Download implements Parcelable {

    private int progress;
    private int  currentFileSize;
    private int  totalFileSize;

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public int  getCurrentFileSize() {
        return currentFileSize;
    }

    public void setCurrentFileSize(int currentFileSize) {
        this.currentFileSize = currentFileSize;
    }

    public int getTotalFileSize() {
        return totalFileSize;
    }

    public void setTotalFileSize(int totalFileSize) {
        this.totalFileSize = totalFileSize;
    }

    @Override
public int describeContents() {
        return 0;
    }

    @Override
public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.progress);
        dest.writeLong(this.currentFileSize);
        dest.writeLong(this.totalFileSize);
    }

    public Download() {
    }

    protected Download(Parcel in) {
        this.progress = in.readInt();
        this.currentFileSize = (int) in.readLong();
        this.totalFileSize = (int) in.readLong();
    }

    public static final Parcelable.Creator<Download> CREATOR = new Parcelable.Creator<Download>() {
        @Override
public Download createFromParcel(Parcel source) {
            return new Download(source);
        }

        @Override
public Download[] newArray(int size) {
            return new Download[size];
        }
    };
}
(3)下載檔案網路介面:注:這裡@Url是傳入完整的的下載URL;不用擷取
/**
 * Created by JokAr on 16/7/5.
 */
public interface DownloadService {


    @Streaming
    @GET
Observable<ResponseBody> download(@Url String url);
}
/**
 * Created by JokAr on 16/7/5.
 */
public class DownloadAPI {
    private static final String TAG = "DownloadAPI";
    private static final int DEFAULT_TIMEOUT = 15;
    public Retrofit retrofit;


    public DownloadAPI(String url, DownloadProgressListener listener) {

        DownloadProgressInterceptor interceptor = new DownloadProgressInterceptor(listener);

        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .retryOnConnectionFailure(true)
                .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();


        retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }

    public void downloadAPK(@NonNull String url, final File file, Subscriber subscriber) {
        Log.d(TAG, "downloadAPK: " + url);

        retrofit.create(DownloadService.class)
                .download(url)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .map(new Func1<ResponseBody, InputStream>() {
                    @Override
public InputStream call(ResponseBody responseBody) {
                        return responseBody.byteStream();
                    }
                })
                .observeOn(Schedulers.computation())
                .doOnNext(new Action1<InputStream>() {
                    @Override
public void call(InputStream inputStream) {
                        try {
                            FileUtils.writeFile(inputStream, file);
                        } catch (IOException e) {
                            e.printStackTrace();

                        }
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


}
(5)然後就是呼叫了: 
 該網路是在service裡完成的
/**
 * Created by JokAr on 16/7/5.
 */
public class DownloadService extends IntentService {
    private static final String TAG = "DownloadService";

    private NotificationCompat.Builder notificationBuilder;
    private NotificationManager notificationManager;

    int downloadCount = 0;

   // private String apkUrl = "http://download.fir.im/v2/app/install/5818acbcca87a836f50014af?download_token=a01301d7f6f8f4957643c3fcfe5ba6ff";
private  String apkUrl="http://123.57.221.150/guquanguanjia_v1.2.apk";
    public DownloadService() {
        super("DownloadService");
    }

    private File outputFile;

    @Override
protected void onHandleIntent(Intent intent) {
       notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("Download")
                .setContentText("Downloading File")
                .setAutoCancel(true);

        notificationManager.notify(0, notificationBuilder.build());

        download();
    }

    private void download() {
        DownloadProgressListener listener = new DownloadProgressListener() {
            @Override
public void update(long bytesRead, long contentLength, boolean done) {
                //不頻繁傳送通知,防止通知欄下拉卡頓
int progress = (int) ((bytesRead * 100) / contentLength);
                if ((downloadCount == 0) || progress > downloadCount) {
                    Download download = new Download();
                    download.setTotalFileSize((int) contentLength);
                    download.setCurrentFileSize((int) bytesRead);
                    download.setProgress(progress);

                   sendNotification(download);
                }
            }
        };
        outputFile = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "file.apk");

        if (outputFile.exists()) {
            outputFile.delete();
        }


        String baseUrl = StringUtils.getHostName(apkUrl);

        new DownloadAPI(baseUrl, listener).downloadAPK(apkUrl, outputFile, new Subscriber() {
            @Override
public void onCompleted() {
                downloadCompleted();
            }

            @Override
public void onError(Throwable e) {
                e.printStackTrace();
                downloadCompleted();
                Log.e(TAG, "onError: " + e.getMessage());
            }

            @Override
public void onNext(Object o) {

            }
        });
    }

    private void downloadCompleted() {
        Download download = new Download();
        download.setProgress(100);
        sendIntent(download);

      notificationManager.cancel(0);
        notificationBuilder.setProgress(0, 0, false);
        notificationBuilder.setContentText("File Downloaded");
        notificationManager.notify(0, notificationBuilder.build());

        //安裝apk
Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
        startActivity(intent);
    }

   private void sendNotification(Download download) {

        sendIntent(download);
        notificationBuilder.setProgress(100, download.getProgress(), false);
        notificationBuilder.setContentText(
                StringUtils.getDataSize(download.getCurrentFileSize()) + "/" +
                        StringUtils.getDataSize(download.getTotalFileSize()));
        notificationManager.notify(0, notificationBuilder.build());
    }

    private void sendIntent(Download download) {

        Intent intent = new Intent(MainActivity.MESSAGE_PROGRESS);
        intent.putExtra("download", download);
        LocalBroadcastManager.getInstance(DownloadService.this).sendBroadcast(intent);
    }

    @Override
public void onTaskRemoved(Intent rootIntent) {
        notificationManager.cancel(0);
    }
}
(6)MainActivity.Class類
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    public static final String MESSAGE_PROGRESS = "message_progress";
    private LocalBroadcastManager bManager;
    private CustomerDialog mDialog;
    Intent intent;

    private AppCompatButton btn_download;
    private ProgressBar progress;
    private TextView progress_text;
    private  Boolean flag=false;

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(MESSAGE_PROGRESS)) {

                Download download = intent.getParcelableExtra("download");
                if (download.getProgress() == 100) {
                    if (mDialog.isShowing())
                    {
                        mDialog.setMessage("下載完成");
                        mDialog.dismiss();
                    }
                    flag=false;

                } else {

                    if (mDialog.isShowing()){
                        int  a=StringUtils.getDataSize((int) download.getCurrentFileSize());
                        int  b=StringUtils.getDataSize((int) download.getTotalFileSize());
                        mDialog.setMax(b);
                        mDialog.setProgress(a);
                    }
                }
            }
        }
    };


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState