今天更新自家apk 发现下载通知栏没提示,下载完成后不自动安装 。 这波尴尬了! 查阅发现:
8.0+版本。因为太久没关注新版本特性,所以导致了这个问题的发生
清单文件权限声明:
<uses-permissionandroid:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
我代码的做法是点击下载的时候就去判断是否开启 《未知来源应用的权限》
private void installProcess() {
boolean haveInstallPermission;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//先获取是否有安装未知来源应用的权限
haveInstallPermission = getPackageManager().canRequestPackageInstalls();
if (!haveInstallPermission) {//没有权限
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("安装应用需要打开未知来源权限,请去设置中开启权限");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startInstallPermissionSettingActivity();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
return;
}
}
//有权限执行安装的方法
....
}
//8.0 跳转到当前包名的设置未知安装
@RequiresApi(api = Build.VERSION_CODES.O)
private void startInstallPermissionSettingActivity() {
//注意这个是8.0新API
Uri packageURI = Uri.parse("package:"+context.getPackageName());
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,packageURI);
startActivityForResult(intent, 10086);
}
//设置回调 ,重新走一遍权限验证
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10086 && resultCode == RESULT_OK) {
installProcess();
}
}
下载过程中.遇到8.0 通知栏 无法显示问题 。。
这是Android O新增的通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道。用户界面将通知渠道称之为通知类别。
先定义一下渠道id 跟渠道name
private static final String YOUR_CHANNEL_ID = "您的id"; private static final String YOUR_CHANNEL_NAME = "您的name";
/**
* 更新notification 为了代码更直观 并未抽取优化
* @param result
* @param msg
* @param progress
*/
private void notifyUser(String result, String msg, int progress){
if (Build.VERSION.SDK_INT>=26){
Notification.Builder notification1 = null;
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
// Toast.makeText(this, mChannel.toString(), Toast.LENGTH_SHORT).show();
notificationManager.createNotificationChannel(mChannel);
notification1 = new Notification.Builder(this);
notification1.setChannelId(id);
notification1.setContentTitle(getString(R.string.app_name));
if(progress>0 && progress<=100){
notification1.setProgress(100,progress,false);
}else{
notification1.setProgress(0, 0, false);
}
notification1.setAutoCancel(true);
notification1.setWhen(System.currentTimeMillis());
notification1.setTicker(result);
notification1.setSmallIcon(R.mipmap.xantico);
notification1 .build();
notification1.setWhen(System.currentTimeMillis());
notification1.setTicker(result);
notification1.setContentIntent(progress>=100 ? getContentIntent() :
PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT));
Notification notification = notification1.build();
notificationManager.notify(0, notification);
}else{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.xantico)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.xantico))
.setContentTitle(getString(R.string.app_name));
if(progress>0 && progress<=100){
builder.setProgress(100,progress,false);
}else{
builder.setProgress(0, 0, false);
}
builder.setAutoCancel(true);
builder.setWhen(System.currentTimeMillis());
builder.setTicker(result);
//进度到达100% 进行安装
builder.setContentIntent(progress>=100 ? getContentIntent() :
PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT));
Notification notification = builder.build();
notificationManager.notify(0, notification);
}
}
Android 7.0以上 无法通过file来获取路径 , 这里需要使用 FileProvider 类
/**
* 进入apk安装程序
* @return
*/
private PendingIntent getContentIntent() {
File apk = new File(filePath);
Log.e("tag", ""+apk +"老纪");
Intent intent = new Intent(Intent.ACTION_VIEW);
//判读版本是否在7.0以上
if (Build.VERSION.SDK_INT >= 24) {
Uri apkUri = FileProvider.getUriForFile(getBaseContext(), "您项目的包名.fileprovider", apk);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(apk), "application/vnd.android.package-archive");
}
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
getBaseContext().startActivity(intent);
return pendingIntent;
}
关于配置 FileProvider 自行百度 下 !!!