public class UpdateManager {
private static final String TAG = "UpdateManager";
private Context context;
public static final int NOTIFICATION_ID = 10;
private NotificationManager mNotifyManager;
private NotificationCompat.Builder mBuilder;
private Notification notification;
private String apkLocalUrl;
private String apkFileName = "XXX.apk";
public UpdateManager(Context context) {
this.context = context;
if(Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)){
apkLocalUrl = Environment.getExternalStorageDirectory().getAbsolutePath() + "/install/";
}else {
apkLocalUrl = Environment.getDataDirectory() + "/install/";
}
}
/**
* 開始更新
* @param downloadURL
*/
public void update(String downloadURL) {
L.e("xxx","downloadURL:"+downloadURL);
if (TextUtils.isEmpty(downloadURL)) {
return;
}
File file = new File(apkLocalUrl,apkFileName);
if(file.exists()){
file.delete();
}
/**
* 這裏寫網絡請求已經處理結果
*/
}
/**
* 初始化通知欄
*/
private void initNotificatin() {
mNotifyManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(context);
notification = mBuilder.setContentTitle(context.getString(R.string.downloading))
.setTicker(context.getString(R.string.start_update))
.setContentText("")
.setSmallIcon(R.mipmap.ic_launcher)
.build();
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
mNotifyManager.notify(NOTIFICATION_ID, notification);
}
/**
* 安裝APK
*/
public void installAPK(File file) {
if (file != null && file.exists()) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
App.mContext.startActivity(intent);
} else {
Toast.makeText(context, context.getString(R.string.install_fail), Toast.LENGTH_LONG).show();
}}
Android 應用更新管理
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.