作为与用户的第一次亲密接触
splash界面的作用:
1、展现App的Logo,给用户的第一印象;一般包括图标和应用名称,或者加一句标语
2、应用程序初始化的操作;如初始化网络,或者获取用户位置信息,设备信息等
3、检查应用程序的版本;用于提醒版本更新等操作
4、检查当前应用程序是否合法注册;
public class AppUtil {
/**
* 获取应用程序名称
*/
public static String getAppName(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* [获取应用程序版本名称信息]
* @param context
* @return 当前应用的版本名称
*/
public static String getVersionName(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
/** 弹出更新对话框
*/
private void showUpdate() {
Builder builder = new Builder(this);
builder.setTitle("发现新版本");
builder.setMessage(Update.DESCRIPTION);
builder.setPositiveButton("立即下载", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startService(new Intent(SplashActivity.this, UpdateService.class));
handler.sendEmptyMessage(ENTER_HOME);
}
});
builder.setNegativeButton("以后再说", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
handler.sendEmptyMessage(ENTER_HOME);
}
});
builder.show();
};
/**通知栏后台下载apk的服务*/
public class UpdateService extends Service {
// 标题
private int titleId = R.string.app_name;
// 文件存储
private File updateDir = null;
private File updateFile = null;
// 通知栏
private NotificationManager updateNotificationManager = null;
private Notification updateNotification = null;
private RemoteViews remoteViews;// 状态栏通知显示的view
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("开始下载");
// 创建文件
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
updateDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
updateFile = new File(updateDir.getPath(), getResources().getString(titleId) + ".apk");
}
// 创建通知栏
this.updateNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
this.updateNotification = new Notification();
this.updateNotification.flags |= Notification.FLAG_AUTO_CANCEL;
// 通知栏视图
remoteViews = new RemoteViews(getPackageName(), R.layout.notification_update);
remoteViews.setImageViewResource(R.id.iv_update_icon, R.drawable.ic_launcher);
updateNotification.contentView = remoteViews;
updateNotification.icon = R.drawable.ic_launcher;// 设置通知消息的图标
updateNotification.tickerText = "正在下载。。。";// 设置通知消息的标题
// 发出通知
updateNotificationManager.notify(0, updateNotification);
// 下载apk
FinalHttp http = new FinalHttp();
String url = Update.APKURL;
http.download(url, updateFile.getAbsolutePath(), new AjaxCallBack<File>() {
@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
// 下载失败
remoteViews.setTextViewText(R.id.tv_update_text, "下载失败");
remoteViews.setProgressBar(R.id.pb_update_progress, 100, 30, false);
updateNotification.contentView = remoteViews;
updateNotificationManager.notify(0, updateNotification);
// 停止服务
stopSelf();
super.onFailure(t, errorNo, strMsg);
}
@Override
public void onLoading(long count, long current) {
int progress = (int) (current * 100 / count);
remoteViews.setTextViewText(R.id.tv_update_text, "已下载" + progress + "%");
remoteViews.setProgressBar(R.id.pb_update_progress, 100, (int) progress, false);
updateNotification.contentView = remoteViews;
updateNotificationManager.notify(0, updateNotification);
super.onLoading(count, current);
}
@Override
public void onSuccess(File t) {
// 点击安装PendingIntent
Uri uri = Uri.fromFile(t);
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setDataAndType(uri, "application/vnd.android.package-archive");
PendingIntent updatePendingIntent = PendingIntent.getActivity(UpdateService.this, 0, installIntent, 0);
updateNotification.defaults = Notification.DEFAULT_SOUND;// 铃声提醒
remoteViews.setTextViewText(R.id.tv_update_text, "下载完成,点击安装");
remoteViews.setProgressBar(R.id.pb_update_progress, 100, 100, false);
updateNotification.contentView = remoteViews;
updateNotification.setLatestEventInfo(getApplicationContext(), "APP", "下载完成,点击安装", updatePendingIntent);
updateNotificationManager.notify(0, updateNotification);
// 停止服务
stopSelf();
super.onSuccess(t);
}
});
return super.onStartCommand(intent, flags, startId);
}
}
