动态权限申请
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);
}
}
}