本帖最后由 HM周一帆 于 2013-3-28 19:59 编辑
package cn.itcast.mobilesafe.ui;
import cn.itcast.mobilesafe.R;
import cn.itcast.mobilesafe.domain.UpdataInfo;
import cn.itcast.mobilesafe.engine.UpdataInfoService;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class SplashActivity extends Activity {
private static final String TAG = "SplashActivity";
private UpdataInfo info;
private TextView tv_splash_version;
private LinearLayout ll_splash_main;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//取消标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
ll_splash_main = (LinearLayout) this.findViewById(R.id.ll_splash_main);
tv_splash_version=(TextView) this.findViewById(R.id.tv_splash_version);
String versiontext = getVersion();
if(isNeedUpdate(versiontext))
{
Log.i(TAG, "弹出升级对话框");
showUpdataDialog();
//AlertDialog.Builder();
}
tv_splash_version.setText(versiontext);
//设置动画 又透明 变成不透明
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
// 动画时间 2秒
aa.setDuration(2000);
ll_splash_main.startAnimation(aa);
//完成全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
/*
* 升级对话框
*/
private void showUpdataDialog() {
AlertDialog.Builder builder = new Builder(this);
builder.setIcon(R.drawable.icon5);
builder.setTitle("升级提醒");
builder.setMessage(info.getDescription());
builder.setCancelable(false);//让用户不能取消对话框
builder.setPositiveButton("确定", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.i(TAG,"下载apk文件"+info.getApkurl());
}
});
builder.setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.i(TAG,"用户取消进入程序主界面");
}
});
builder.create().show();
}
/*version 当前客户端的版本信号
* return 是否需要更新
*/
private boolean isNeedUpdate(String versiontext) {
UpdataInfoService service = new UpdataInfoService(this);
try
{
info = service.getUpdataInfo(R.string.updataurl);
String version = info.getVersion();
if(versiontext.equals(version))
{
Log.i(TAG, "版本号相同 ,无需升级,进入主界面");
return false;
}
else {
Log.i(TAG,"版本号不同 ,请升级");
return true;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(this, "获取更新信息异常", 0).show();
Log.i(TAG,"获取更新信息异常,进入主界面");
return false ;
}
//获取当前应用版本号
private String getVersion(){
try
{
PackageManager manager = getPackageManager();
PackageInfo info =manager.getPackageInfo(getPackageName(), 0);
return info.versionName;
} catch (Exception e) {
return("版本号未知");
}
}
}
这个是一个安卓项目里的一段代码 这段代码有个功能是为了跳出了一个升级版本提醒栏 为什么不出现呢 |
|