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 欢迎大家的加入.

点击下载源码

目录
相关文章
|
4月前
|
JavaScript 前端开发 Java
[Android][Framework]系统jar包,sdk的制作及引用
[Android][Framework]系统jar包,sdk的制作及引用
102 0
|
1月前
|
Java Linux API
Android SDK
【10月更文挑战第21天】
71 1
|
2月前
|
程序员 开发工具 Android开发
Android|使用阿里云推流 SDK 实现双路推流不同画面
本文记录了一种使用没有原生支持多路推流的阿里云推流 Android SDK,实现同时推送两路不同画面的流的方法。
63 7
|
4月前
|
开发工具 Android开发
Android项目架构设计问题之SDK内部减少每次回调时的冗余判断逻辑如何解决
Android项目架构设计问题之SDK内部减少每次回调时的冗余判断逻辑如何解决
42 0
|
4月前
|
Java API 开发工具
Android项目架构设计问题之为SDK添加新的回调支持如何解决
Android项目架构设计问题之为SDK添加新的回调支持如何解决
27 0
|
7月前
|
JavaScript Java Maven
云效产品使用常见问题之android sdk 构建出aar后,上传到私有maven仓库失败如何解决
云效作为一款全面覆盖研发全生命周期管理的云端效能平台,致力于帮助企业实现高效协同、敏捷研发和持续交付。本合集收集整理了用户在使用云效过程中遇到的常见问题,问题涉及项目创建与管理、需求规划与迭代、代码托管与版本控制、自动化测试、持续集成与发布等方面。
|
7月前
|
安全 开发工具 Android开发
几个Flutter常见诊断错误与解决Android toolchain - develop for Android devices X Unable to locate Android SDK
几个Flutter常见诊断错误与解决Android toolchain - develop for Android devices X Unable to locate Android SDK
2043 0
|
4月前
|
开发工具 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
182 4
解决Android运行出现NDK at /Library/Android/sdk/ndk-bundle did not have a source.properties file
|
4月前
|
Dart 开发工具 Android开发
Android Studio导入Flutter项目提示Dart SDK is not configured
Android Studio导入Flutter项目提示Dart SDK is not configured
356 4
|
4月前
|
开发工具 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
160 2