動态權限申請
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
public class PermisionUtils {
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
/**
* Checks if the app has permission to write to device storage
* If the app does not has permission then the user will be prompted to
* grant permissions
*
* @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
}
}
}
從伺服器擷取版本 還有下載下傳路徑
如果有新版本則彈窗提示使用者更新
使用者确認後
verifyStoragePermissions(CheapVersionActivity.this); 權限申請
DownloadManager dManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = null;
String mesg = "";
try {
uri = Uri.parse(jsonObject.get("url").toString());//下載下傳路徑 http://www.xxx.com/xxx.apk
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(CheapVersionActivity.this, "正在下載下傳,請稍候", Toast.LENGTH_SHORT).show();
DownloadManager.Request req = new DownloadManager.Request(uri);
//設定網絡狀态下進行更新
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI);
//下載下傳中和下載下傳完後都·顯示通知欄
req.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
//使用系統預設的下載下傳路徑 此處為應用内 /android/data/packages ,是以相容7.0 8.0
req.setDestinationInExternalFilesDir(CheapVersionActivity.this, Environment.DIRECTORY_DOWNLOADS, "apk名稱.apk");
//通知欄标題
req.setTitle("通知欄标題");
//通知欄描述資訊
req.setDescription("下載下傳中");
//設定類型為.apk
req.setMimeType("application/vnd.android.package-archive");
req.setVisibleInDownloadsUi(true);
SharedPreferences userPreference = getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = userPreference.edit();
//擷取下載下傳任務ID
DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadId=dm.enqueue(req); //long類型
editor.putString("versionId",downloadId+"" );
editor.commit();
registerReceiver(new UpdataBroadcastReceiver(), new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
manifest.xml中
<receiver android:name=".util.UpdataBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
建立 UpdataBroadcastReceiver.class 全局消息
import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import com.tencent.mm.opensdk.utils.Log;
public class UpdataBroadcastReceiver extends BroadcastReceiver {
private SharedPreferences sp;
@SuppressLint("NewApi")
public void onReceive(Context context, Intent intent) {
sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
DownloadManager dManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// //根據id判斷如果檔案已經下載下傳成功傳回儲存檔案的路徑
Uri downloadFileUri = dManager.getUriForDownloadedFile(Long.parseLong(sp.getString("versionId","0")));
Log.d("DownloadManager", "apk存儲路徑 : " + downloadFileUri);
install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
if (downloadFileUri != null) {
if ((Build.VERSION.SDK_INT >= 24)) {//判讀版本是否在7.0以上
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//添加這一句表示對目标應用臨時授權該Uri所代表的檔案
// 不添加無法安裝 或者出現解析包異常
}
context.startActivity(install);
}
}
}