Android 本身實現更新下載自動安裝

一、一些公司開發完一款App以後可能並不會去上架App商店,但過後期也須要定時進行維護更新,因此會選擇把打包好的apk 發佈到本身的服務器,而後在數據庫建一個版本號的表,而後剩下的就交給你android開發了,android本身要實現版本檢測更新,因爲android自帶的DownloadManager 就能夠實現下載功能,用起來就會很簡單了,不用再寫不少下載等相關代碼了,不過在下載完有的是在通知欄通知,而後用戶本身手動點擊進行安裝,有的是下載完本身就進入安裝狀態了,用戶只須要確認安裝就能夠了,可是因爲一些高版本的系統和低版本的自動安裝不一樣,這裏就簡單介紹一下,然你們能很快的處理這個事情,我就是在這個自動安裝這塊弄了有好幾天不知道怎麼回事,先總結一下:android

二、這裏就從開始下載開始,那些版本號的請求就不提了,,數據庫

    這裏咱們能夠先寫一個工具類Util,而後頂一個靜態方法downLoadApk();服務器

   eg:app

/**
* 更新下載apk
* @param context 上下文對象
* @param title 程序的名字
* @param url 下載的url地址
*
*/ide

public static long downLoadApk(Context context,String title,String url){工具

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS,"ausee.apk");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// 設置 Notification 信息
request.setTitle(title);
request.setDescription("下載完成後請點擊打開");
request.setVisibleInDownloadsUi(true);
request.allowScanningByMediaScanner();
request.setMimeType("application/vnd.android.package-archive");ui

// 實例化DownloadManager 對象
DownloadManager downloadManager = (DownloadManager) MyApp.getContext().getSystemService(Context.DOWNLOAD_SERVICE);
final long refrence = downloadManager.enqueue(request);this

return refrence;
}
上面的那個靜態方法就能夠實現下載了,把url傳過去就OK了;下面就來處理剩下的,你們注意上面的方法會返回一個long類型的值,
 你們在activity調用這個方法的時候,拿到這個返回值,而後在activity裏面建一個廣播接收器,由於上面的返回值是DownloadManager 下載完後返回的一個下載id,自帶的,每個下載任務都會返回一個惟一的id,而且會發一條廣播,這裏我在activity裏面定義一個方法:listener(id),並建一個廣播接受器; 以下url

private void listener(final long Id) {
// 註冊廣播監聽系統的下載完成事件。
IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
DownloadManager manager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
  // 這裏是經過下面這個方法獲取下載的id,
long ID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
// 這裏把傳遞的id和廣播中獲取的id進行對比是否是咱們下載apk的那個id,若是是的話,就開始獲取這個下載的路徑
if (ID == Id) {spa

DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(Id);

Cursor cursor = manager.query(query);
if (cursor.moveToFirst()){
// 獲取文件下載路徑
String fileName = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

// 若是文件名不爲空,說明文件已存在,則進行自動安裝apk
if (fileName != null){

openAPK(fileName);

}
}
cursor.close();
}
}
};
registerReceiver(broadcastReceiver, intentFilter);
}
下面是上面的自動打開apk的方法:
/**
* 安裝apk
* @param fileSavePath
*/
private void openAPK(String fileSavePath){
File file=new File(Uri.parse(fileSavePath).getPath());
String filePath = file.getAbsolutePath();
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//判斷版本大於等於7.0
// 生成文件的uri,,
  // 注意 下面參數com.ausee.fileprovider 爲apk的包名加上.fileprovider,
data = FileProvider.getUriForFile(LoginActivity.this, "com.ausee.fileprovider", new File(filePath));
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);// 給目標應用一個臨時受權
} else {
data = Uri.fromFile(file);
}

intent.setDataAndType(data, "application/vnd.android.package-archive");
startActivity(intent);
}
上面基本就能夠了,可是上面的那個參數com.ausee.fileprovider,這個須要注意,進行下面的配置
先在項目res文件下新建一個文件夾名字爲 xml ;後新建一個xml的文件:file_paths.xml;
xml中內容爲:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="name" path="."/>
</paths>
這裏寫好以後,下面在manifest裏面配置一個provider標籤:這麼寫就能夠:

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.ausee.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
仔細看上面的 android:authorities="com.ausee.fileprovider" (com.ausee爲你本身的包名),這個內容和前面的那個參數同樣的吧?就是由於這裏,這裏的內容也是那樣填寫的,包名加上一個fileprovider 就能夠了,而後在meta-data 裏面把剛開配置的xml文件配置進來就能夠!

 

須要加個 android.intent.category.DEFAULT

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

好了,到這裏就完工了,快去試試吧!

相關文章
相關標籤/搜索