Android应用程序实现自动更新功能3_UI进度条设计

前面已经介绍了服务器和客户端的实现,本节介绍UI上的设计,UI设计中主要还是进度条的设计

在应用中需要获取当前版本的信息,代码如下:

private String getVersionCode(Context mcontext){
		 String versionCode = null;
		 
	    try
	    {

	       versionCode = mcontext.getPackageManager().getPackageInfo("com.example.apkupgrade", 0).versionName;
	    } catch (NameNotFoundException e)
	    {
	        e.printStackTrace();
	    }
	    return versionCode;
	}
主程序的UI界面activity_apkupgrade.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	
    <TextView
		android:id="@+id/textview"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center_horizontal"
		android:text="@string/version_info"
		android:textSize="25sp"
		/>
    
	<Button
		android:id="@+id/btnUpdate"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:text="检查更新"
		/>
	<Button
		android:id="@+id/btnInstall"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:text="安装"
		/>
</LinearLayout>
主界面为:


点击检查更新后就会通过http获取服务器的版本信息文件version.xml文件,然后弹出服务器的信息窗口



这种弹出方式使用AlertDialog的实现方式,实现代码如下:

AlertDialog.Builder builder = new Builder(ApkupgradeActivity.this);
		builder.setTitle(R.string.soft_update_state);
		final LayoutInflater inflater = LayoutInflater.from(ApkupgradeActivity.this);
		View v = inflater.inflate(R.layout.update_version, null);
		TextView curversion = (TextView) v.findViewById(R.id.currversion);
		curversion.setText(curversion.getText()+getVersionCode(ApkupgradeActivity.this));
		TextView serversion = (TextView) v.findViewById(R.id.serversion);
		serversion.setText(serversion.getText()+hashMap.get("version"));
		TextView sername = (TextView) v.findViewById(R.id.sername);
		sername.setText(sername.getText()+hashMap.get("name"));
		TextView serurl = (TextView) v.findViewById(R.id.serurl);
		serurl.setText(serurl.getText()+hashMap.get("url"));
		
		builder.setView(v);
		
		builder.setPositiveButton(R.string.soft_update_updatebtn, new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
				// 显示下载对话框
				showDownloadDialog();
			}
		});

		builder.setNegativeButton(R.string.soft_update_later, new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
			}
		});
		Dialog noticeDialog = builder.create();
		noticeDialog.show();
然后点击立即更新就会进入到下载的提示界面:

下载弹出窗口的代码实现:

private void showDownloadDialog(){

		AlertDialog.Builder builder = new Builder(ApkupgradeActivity.this);
		builder.setTitle(R.string.soft_updating);
		final LayoutInflater inflater = LayoutInflater.from(ApkupgradeActivity.this);
		View v = inflater.inflate(R.layout.softupdate_progress, null);
		mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
		prostate = (TextView) v.findViewById(R.id.prostate);
		
		builder.setView(v);
		
		builder.setNegativeButton(R.string.soft_update_cancel, new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
				// 设置取消状态
				cancelUpdate = true;
			}
		});
		mDownloadDialog = builder.create();
		mDownloadDialog.show();
		// 现在文件
		//downloadApk();
		new downloadApkThread().start();
		
	 }
其中的布局文件softupdate_progress.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
	<FrameLayout 
	android:layout_width="fill_parent"
	android:layout_height="80dp"
	android:background="@color/SlateGray"
	android:orientation="horizontal" >
	<TextView
	    android:id="@+id/prostate"
	    android:layout_marginLeft="20dip"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="0%100"
		android:textColor="@color/GhostWhite"
		/>
		<ProgressBar
		android:id="@+id/update_progress"
		android:layout_width="500dp"
		android:layout_height="wrap_content"
		style="?android:attr/progressBarStyleHorizontal"
		android:progressDrawable="@drawable/progressbar_style"
		android:progress="0" android:max="100" android:secondaryProgress="100" 
		android:layout_gravity="center" />
	</FrameLayout>
</LinearLayout>
安卓的UI布局中,我还是喜欢使用帧布局。系统自带的进度条风格实在是太丑了,自己加入了drawable/progressbar_style.xml文件。

<?xml version="1.0" encoding="UTF-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

	<item android:id="@android:id/background" >
	<shape>
		<corners android:radius="5.0dip" />
	<gradient android:startColor="#656666" android:endColor="#dbdedf" android:angle="270.0" android:centerY="0.75" android:centerColor="#bbbbbc" />
	</shape>
	</item>
	<item android:id="@android:id/secondaryProgress">
	<clip>
		<shape>
		<corners android:radius="0.0dip" />
		<gradient android:startColor="@color/LightSlateGray" android:endColor="@color/LightSlateGray" android:angle="90.0" android:centerY="0.75" android:centerColor="@color/LightSlateGray" />
		</shape>
	</clip>
	</item>
	<item android:id="@android:id/progress">
	<clip>
		<shape>
		<corners android:radius="0.0dip" />
		<gradient android:startColor="@color/LightSkyBlue" android:endColor="@color/SteelBlue" android:angle="270.0" />
		</shape>
	</clip>
	</item>
</layer-list>
相关的color.xml文件有:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <color name="GhostWhite">#FFF8F8FF</color>
    <color name="blue">#ff0000ff</color>
    <color name="dark">#ffb2b2b2</color>
    <color name="green">#ffb2ffb2</color>
    <color name="detail_point">#ffffaa00</color>
    <color name="detail_main_text">#ff999999</color>
    <color name="black">#ff000000</color>
    <color name="red">#ffee2200</color>
    <color name="blue_user">#ff00a8ff</color>
    <color name="dark_404040">#ff404040</color>
    <color name="DeepSkyBlue">#FF00BFFF</color>
    <color name="SlateGray">#FF708090</color>
    <color name="DimGray">#FF696969</color>
    <color name="LightSlateGray">#FF778899</color>
    <color name="SteelBlue">#FF4682B4</color>
    <color name="LightSkyBlue">#FF87CEFA</color>
</resources>
安装使用的是设备自带的第三方程序,效果如下:


点击确定后,就可以看见应用程序所获取的权限信息。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔波的IT人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值