installApk();//下载成功 打开安装界面
stopSelf();//结束服务
sendBroadcast(new Intent().setAction(“android.intent.action.loading_over”));//发送下载结束的广播
}
@Override
public void onFailure(HttpException arg0, String arg1) {
Toast.makeText(LoadingService.this,“下载失败…”,Toast.LENGTH_SHORT).show();
sendBroadcast(new Intent().setAction(“android.intent.action.loading_over”));//发送下载结束的广播
nm.cancel(R.layout.notification_item);
stopSelf();
}
});
}
/**
- 安装下载的新版本
*/
protected void installApk() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
File file = new File(path);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// 7.0 以上
try {
Uri apkUri = FileProvider.getUriForFile(this, “com.jw.fileprovider”, file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setDataAndType(apkUri, “application/vnd.android.package-archive”);
}catch (Exception e){
}
} else {
// 7.0以下
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, “application/vnd.android.package-archive”);
}
this.startActivity(intent);
}
private void createNotification(final long total, final long current){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,“default”);
builder.setSmallIcon(R.mipmap.ic_launcher)//必须要设置这个属性,否则不显示
;
RemoteViews contentView = new RemoteViews(this.getPackageName(),R.layout.notification_item);
contentView.setProgressBar(R.id.progress, (int)total, (int)current, false);
builder.setOngoing(true);//设置左右滑动不能删除
// Notification notification = builder.build();
// notification.contentView = contentView;
String id = “channel_001”;
String name = “name”;
Notification notification = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
nm.createNotificationChannel(mChannel);
notification = new Notification.Builder(LoadingService.this,“default”)
.setChannelId(id)
.setContentTitle(“客户端更新”)
.setContentText(“更新完毕”)
.setSmallIcon(R.mipmap.ic_launcher).build();
notification.contentView = contentView;
} else {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(LoadingService.this,“default”)
.setContentTitle(“更新了”)
.setContentText(“更新了xxxxxxxxx内容”)
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true)
;//无效
notification = notificationBuilder.build();
notification.contentView = contentView;
}
nm.notify(R.layout.notification_item,notification);//发送通知
}
}
Utils.java
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
public class Utils {
/**
-
获取版本号
-
@throws PackageManager.NameNotFoundException
*/
public static String getVersionName(Context context) throws PackageManager.NameNotFoundException {
// 获取packagemanager的实例
PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);</