易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
Android开发:启动应用的数据库初始化和版本更新
public
class
SplashActivity
extends
Activity {
private
static
final
String TAG =
"mainactivity"
;
private
static
final
int
NEED_UPDATE =
1
;
private
static
final
int
CONNECT_SERVER_ERROR =
2
;
private
static
final
int
SHOW_MESSAGE=
3
;
private
static
final
int
HIDE_MESSAGE=
4
;
UpdateInfo info;
AddressService addressService;
TextView tv_copydb;
private
Handler handler =
new
Handler() {
@Override
public
void
handleMessage(Message msg) {
super
.handleMessage(msg);
switch
(msg.what) {
case
NEED_UPDATE:
showUpdateDialog();
break
;
case
CONNECT_SERVER_ERROR:
showErrorDialog();
break
;
case
SHOW_MESSAGE:
tv_copydb.setVisibility(View.VISIBLE);
break
;
case
HIDE_MESSAGE:
tv_copydb.setVisibility(View.INVISIBLE);
break
;
}
}
private
void
showErrorDialog() {
AlertDialog.Builder builder =
new
Builder(SplashActivity.
this
);
builder.setTitle(
"错误"
);
builder.setMessage(
"连接服务器失败"
);
builder.setPositiveButton(R.string.ok,
new
OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
which) {
//finish();
//临时修改,不连接服务器也能进程序主界面
Intent intent =
new
Intent(SplashActivity.
this
,MainScreenActivity.
class
);
startActivity(intent);
finish();
}
});
builder.create().show();
}
/***
* 因为在非UI线程里面不能创建对话框,所以我们需要用handler 和 message 来实现创建对话框
* */
private
void
showUpdateDialog() {
AlertDialog.Builder builder =
new
Builder(SplashActivity.
this
);
//错误AlertDialog.Builder builder = new Builder(getApplicationContext());
//mainactivity.this 是 getApplicationContext()的子集.
//mainactivity 里面有一些参数 比如说当前activity 的windowlayout的一些参数是 getApplicationContext()所没有的
builder.setTitle(R.string.please_update);
builder.setMessage(info.getDescription());
builder.setPositiveButton(R.string.ok,
new
OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
which) {
Log.i(TAG,
"调用下载方法"
);
final
ProgressDialog pd =
new
ProgressDialog(SplashActivity.
this
);
pd.setMessage(getApplicationContext().getResources().getString(R.string.downing));
pd.show();
try
{
new
Thread(){
//开辟线程下载服务器更新的apk文件
public
void
run() {
File file;
try
{
file = DownLoadService.getFile(info.getUrl());
//解析的url
install(file);
pd.dismiss();
}
catch
(Exception e) {
e.printStackTrace();
}
}}.start();
}
catch
(Exception e) {
e.printStackTrace();
}
}
//确定了后开始安装,替换掉原来的了
private
void
install(File file) {
Intent intent =
new
Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.Android.package-archive"
);
finish();
//自己安装完了关闭AlertDialog
startActivity(intent);
}
});
builder.setNegativeButton(R.string.cancle,
new
OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
which) {
Log.i(TAG,
"用户取消"
);
Intent intent =
new
Intent(SplashActivity.
this
,MainScreenActivity.
class
);
startActivity(intent);
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
};
//handler结束
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
this
.requestWindowFeature(Window.FEATURE_NO_TITLE);
this
.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splashactivity);
tv_copydb = (TextView) findViewById(R.id.copydatabase);
addressService =
new
AddressService(
this
);
boolean
isexist = addressService.isExist();
if
(isexist){
//数据库存在不做任何操作
new
Thread(
new
CheckVersionTask()).start();
}
else
{
new
Thread(
new
CopyDBTask()).start();
}
AlphaAnimation aa =
new
AlphaAnimation(
0
.1f,
1
.0f);
aa.setDuration(
5000
);
RelativeLayout rl = (RelativeLayout)
this
.findViewById(R.id.rl_splash);
rl.startAnimation(aa);
}
//初始化完address.db数据库,不存在的拷贝任务
private
class
CopyDBTask
implements
Runnable{
public
void
run() {
try
{
sendShowTextMessage();
addressService.copyDataBase();
sendHideTextMessage();
new
Thread(
new
CheckVersionTask()).start();
}
catch
(Exception e) {
e.printStackTrace();
}
}
//向handle发消息,更新textview,显示正在下载
private
void
sendShowTextMessage() {
Message msg =
new
Message();
msg.what = SHOW_MESSAGE;
handler.sendMessage(msg);
}
//下载完后更新textview为不可见
private
void
sendHideTextMessage() {
Message msg =
new
Message();
msg.what = HIDE_MESSAGE;
handler.sendMessage(msg);
}
}
//开辟线程检查是否需要更新升级 (解析服务器xml,判断服务器版本号与本地版本确定是否需要升级)
private
class
CheckVersionTask
implements
Runnable {
public
void
run() {
String path = getApplicationContext().getResources().getString(
R.string.server_url);
try
{
URL url =
new
URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(
5000
);
Thread.sleep(
4000
);
InputStream ins = conn.getInputStream();
info = UpdateInfoParser.getUpdateInfo(ins);
String version = getApplicationContext().getResources()
.getString(R.string.version);
if
(version.equals(info.getVersion())) {
Log.i(TAG,
"版本相同,无需升级"
);
Intent intent =
new
Intent(SplashActivity.
this
,MainScreenActivity.
class
);
startActivity(intent);
finish();
}
else
{
Log.i(TAG,
"版本不相同,需要升级"
);
sendUpdateMessage();
}
}
catch
(Exception e) {
Message msg =
new
Message();
msg.what = CONNECT_SERVER_ERROR;
handler.sendMessage(msg);
e.printStackTrace();
}
}
private
void
sendUpdateMessage() {
Message msg =
new
Message();
msg.what = NEED_UPDATE;
handler.sendMessage(msg);
}
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图