Android接入百度自动更新SDK

简介:

一:前言

公司的app,上传到百度应用市场,然后说必须要接入百度的自动更新sdk才能上架,于是从百度官网上去下载jar包,下载的时候必须要带上数据统计,如果使用自动的jar包,还需要带上广告联盟,坑爹啊,有木有。我下载下来把其他无关的jar包根so文件删掉了.把百度的demo也进行精简了一下.只留下了自动更新必须要用到的.

二:效果图如下


三:代码如下

AutoUpdateActivity.java  代码删的就剩下整个Activity了,其实调用百度sdk还挺简单的,一句话就能搞定,然后再监听回调函数.

 

/**
 * 百度自动更新
 * @author ansen 
 * @create time 2015-11-07
 */
public class AutoUpdateActivity extends Activity implements View.OnClickListener {
	private TextView txt_log;
	private ProgressDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.autoupdate_main);
		findViewById(R.id.btn_ui).setOnClickListener(this);
		findViewById(R.id.btn_silence).setOnClickListener(this);
		findViewById(R.id.btn_as).setOnClickListener(this);
		findViewById(R.id.btn_noui).setOnClickListener(this);
		txt_log = (TextView) findViewById(R.id.txt_log);
		dialog = new ProgressDialog(this);
		dialog.setIndeterminate(true);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_ui://UI更新
			txt_log.setText("");
			dialog.show();
			BDAutoUpdateSDK.uiUpdateAction(this, new MyUICheckUpdateCallback());
			break;
		case R.id.btn_silence:
			txt_log.setText("");
			BDAutoUpdateSDK.silenceUpdateAction(this);
			break;
		case R.id.btn_as:
			txt_log.setText("");
			dialog.show();
			BDAutoUpdateSDK.asUpdateAction(this, new MyUICheckUpdateCallback());
			break;
		case R.id.btn_noui:
			txt_log.setText("");
			dialog.show();
			BDAutoUpdateSDK.cpUpdateCheck(this, new MyCPCheckUpdateCallback());
			break;
		}
	}
	
	@Override
	protected void onDestroy() {
		dialog.dismiss();
		super.onDestroy();
	}
	
	private class MyUICheckUpdateCallback implements UICheckUpdateCallback {
		@Override
		public void onCheckComplete() {
			dialog.dismiss();
		}
	}
	
	private class MyCPCheckUpdateCallback implements CPCheckUpdateCallback {
		@Override
		public void onCheckUpdateCallback(AppUpdateInfo info, AppUpdateInfoForInstall infoForInstall) {
			if(infoForInstall != null && !TextUtils.isEmpty(infoForInstall.getInstallPath())) {
				txt_log.setText(txt_log.getText() + "\n install info: " + infoForInstall.getAppSName() + ", \nverion=" + infoForInstall.getAppVersionName() + ", \nchange log=" + infoForInstall.getAppChangeLog());
				txt_log.setText(txt_log.getText() + "\n we can install the apk file in: " + infoForInstall.getInstallPath());
				BDAutoUpdateSDK.cpUpdateInstall(getApplicationContext(), infoForInstall.getInstallPath());
			}else if(info != null) {
				BDAutoUpdateSDK.cpUpdateDownload(AutoUpdateActivity.this, info, new UpdateDownloadCallback());
			}else {
				txt_log.setText(txt_log.getText() + "\n no update.");
			}
			dialog.dismiss();
		}

	}
	
	private class UpdateDownloadCallback implements CPUpdateDownloadCallback {
		@Override
		public void onDownloadComplete(String apkPath) {
			txt_log.setText(txt_log.getText() + "\n onDownloadComplete: " + apkPath);
			BDAutoUpdateSDK.cpUpdateInstall(getApplicationContext(), apkPath);
		}

		@Override
		public void onStart() {
			txt_log.setText(txt_log.getText() + "\n Download onStart");
		}

		@Override
		public void onPercent(int percent, long rcvLen, long fileSize) {
			txt_log.setText(txt_log.getText() + "\n Download onPercent: " + percent + "%");
		}

		@Override
		public void onFail(Throwable error, String content) {
			txt_log.setText(txt_log.getText() + "\n Download onFail: " + content);
		}

		@Override
		public void onStop() {
			txt_log.setText(txt_log.getText() + "\n Download onStop");
		}
	}
}


autoupdate_main.xml   AutoUpdateActivity加载的布局文件.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dip"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="${packageName}.${activityClass}" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        
        <Button 
	        android:id="@+id/btn_ui"
	        android:layout_width="match_parent"
	        android:layout_height="40dip"
	        android:text="UI更新"/>
	
	    <Button 
	        android:id="@+id/btn_silence"
	        android:layout_width="match_parent"
	        android:layout_height="40dip"
	        android:text="静默更新"
	        />
	    
	    <Button 
	        android:id="@+id/btn_as"
	        android:layout_width="match_parent"
	        android:layout_height="40dip"
	        android:text="百度助手更新"
	        />
	
	    <Button 
	        android:id="@+id/btn_noui"
	        android:layout_width="match_parent"
	        android:layout_height="40dip"
	        android:text="无UI更新"
	        />
    </LinearLayout>
    
    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <TextView 
	        android:id="@+id/txt_log"
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        />
    </ScrollView>
    
</LinearLayout>

四.注意事项

1.需要引用另外一个项目,另外一个项目里面必须有两个百度的jar包.BDAutoUpdate_APPX_SDK_20150826.jar跟need_lib.jar

2.需要在AndroidManifest.xml中配置appid,appkey,还有弹窗显示的Activity.

 

        <!-- 百度自动更新SDK   Appid配置,AppKey配置-->
		<meta-data android:name="BDAPPID" android:value="3067515"/>
    		<meta-data android:name="BDAPPKEY" android:value="f3Os4GAOqxgm79GqbnkT9L8T"/>
        <!-- 百度自动更新SDK -->
        <activity
            android:name="com.baidu.autoupdatesdk.ConfirmDialoigActivity"
            android:exported="false"
            android:screenOrientation="sensor"
            android:theme="@style/bdp_update_dialog_style_fullscreen"/>

3.appid跟appkey是官网申请的  http://app.baidu.com/value/sdkservice?f=9

推荐下自己创建的android QQ群:202928390 欢迎大家的加入.

点击下载源码

目录
相关文章
|
2月前
|
JavaScript 前端开发 Java
[Android][Framework]系统jar包,sdk的制作及引用
[Android][Framework]系统jar包,sdk的制作及引用
43 0
|
2月前
|
开发工具 Android开发
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
140 4
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
|
2月前
|
开发工具 图形学 Android开发
Unity与安卓丨unity报错:SDK Tools version 0.0 < 26.1.1
Unity与安卓丨unity报错:SDK Tools version 0.0 < 26.1.1
|
2月前
|
开发工具 Android开发
Flutter: Android SDK not found at this location,Android Studio not found at xxx
Flutter: Android SDK not found at this location,Android Studio not found at xxx
122 2
|
2月前
|
开发工具 Android开发
Android项目架构设计问题之SDK内部减少每次回调时的冗余判断逻辑如何解决
Android项目架构设计问题之SDK内部减少每次回调时的冗余判断逻辑如何解决
21 0
|
2月前
|
Java API 开发工具
Android项目架构设计问题之为SDK添加新的回调支持如何解决
Android项目架构设计问题之为SDK添加新的回调支持如何解决
18 0
|
7天前
|
IDE Android开发 iOS开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
【9月更文挑战第27天】在移动应用开发的世界中,Android和iOS是两个主要的操作系统平台。每个系统都有其独特的开发环境、工具和用户群体。本文将深入探讨这两个平台的关键差异点,并分析这些差异如何影响应用的性能、用户体验和最终的市场表现。通过对比分析,我们将揭示选择正确的开发平台对于确保项目成功的重要作用。
|
20天前
|
Android开发 开发者 Kotlin
探索安卓开发中的新特性
【9月更文挑战第14天】本文将引导你深入理解安卓开发领域的一些最新特性,并为你提供实用的代码示例。无论你是初学者还是经验丰富的开发者,这篇文章都会给你带来新的启示和灵感。让我们一起探索吧!
|
4天前
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
22 7
|
8天前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台解决方案
【9月更文挑战第27天】在移动应用开发的广阔天地中,安卓和iOS两大操作系统如同双子星座般耀眼。开发者们在这两大平台上追逐着创新的梦想,却也面临着选择的难题。如何在保持高效的同时,实现跨平台的开发?本文将带你探索跨平台开发的魅力所在,揭示其背后的技术原理,并通过实际案例展示其应用场景。无论你是安卓的忠实拥趸,还是iOS的狂热粉丝,这篇文章都将为你打开一扇通往跨平台开发新世界的大门。
下一篇
无影云桌面