Android 自动更新模块

一.Umeng 自动更新的相关注意事项:

 

1.登陆友盟网站http://www.umeng.com/

2.注册企业会员,邮箱激活,登陆。

3.点击左边的添加要更新到的应用版本。

 


4.填写应用的基本信息,获取AppKey值

 

 

 

 

5.点击自动更模块

 

 

 

下载自动更新的SDK,上传一个比目前安装的App版本更高的App,目的是要进行自动更新的测试。

 

6.将下载好的SDK解压,sample,lib,res三个部分。

 

 

sample 是例子,

lib,是自动更新的架包,对于jar的类库,非常简单,只要在项目根目录下新建一个libs目录,然后把jar复制进去,在jar上点击右键,选择Add as library,即可完成依赖的添加。

res是一些布局和资源文件。将SDK提供的res文件夹拷入工程目录下, 和工程本身res目录合并。请不要随便删除其中的文件。

提示

友盟SDK提供的资源文件都以umeng_开头。

 

 

7.配置AndroidManifest.xml

 

打开AndroidManifest.xml,添加SDK需要的权限到<manifest>标签下:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

android.permission.WRITE_EXTERNAL_STORAGE 权限允许将下载的apk保存到sd卡中。
android.permission.ACCESS_NETWORK_STATE 权限允许检查网络状态,从而根据不同网络环境决定何种下载策略,务必添加该权限。

添加APPKEY和渠道到<application>标签下: (如果已经集成了统计SDK等友盟其他服务,不需要重复添加APPKEY)

<meta-data android:value="YOUR APP KEY" android:name="UMENG_APPKEY"/>
<meta-data android:value="Channel ID" android:name="UMENG_CHANNEL"/>

UMENG_APPKEY:用来定位该应用的唯一性,用您该应用的UMENG APPKEY,替换value中的"YOUR APP KEY"。
UMENG_CHANNEL:用来标注应用推广渠道,不同渠道可以上传不同更新包,您可以使用20位以内的英文和数字为渠道定名,替换value中的"Channel ID"。如果不改动,将代表默认渠道。(特别提示:如果需要使用友盟自动更新多渠道更新,必须先集成友盟统计SDK

渠道ID

请参考第5节如何上传APK。

 添加Service和Activity到<application>标签下: (请注意:v2.4的SDK中,对话框改为Activity实现)

注意

com.umeng包名可能有变,如果不能下载,请检查包名,替换成正确的包名。

<service
    android:name="com.umeng.update.net.DownloadingService"
    android:process=":DownloadingService" >
</service>
<activity 
    android:name="com.umeng.update.UpdateDialogActivity"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" >
</activity>

7.代码

 

 /**
     * Check Version
     */
    private void checkVersion() {
        UmengUpdateAgent.setUpdateOnlyWifi(true);
        UmengUpdateAgent.setUpdateAutoPopup(false);
        UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
            @Override
            public void onUpdateReturned(int updateStatus, UpdateResponse updateInfo) {
                if (updateStatus == 0 && updateInfo != null) {
                    showUpdateDialog(updateInfo.path, updateInfo.updateLog);
                } else {
                    initView();
                    initData();
                }
                // case 0: // has update
                // case 1: // has no update
                // case 2: // none wifi
                // case 3: // time out
            }
        });
        UmengUpdateAgent.update(this);
    }

    /**
     * Achieve version update by Umeng
     *
     * @param downloadUrl update url
     * @param message     update infornation
     */

    private void showUpdateDialog(final String downloadUrl, final String message) {
        // 1. Layout change yo View
        LayoutInflater inflater = LayoutInflater.from(this);
        RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.alert_dialog, null);
        // 2. New dialog object
        final Dialog dialog = new AlertDialog.Builder(SplashActivity.this).create();
        dialog.setCancelable(false);
        dialog.show();
        dialog.getWindow().setContentView(layout);
        // 3. Message
        TextView dialog_msg = (TextView) layout.findViewById(R.id.dialog_msg);
        dialog_msg.setText(message);
        // 4. Ensure Button
        Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);
        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                try {
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(downloadUrl)));
                } catch (Exception ex) {

                }
                SplashActivity.this.finish();
            }
        });

        // 5. Cancel Button
        Button btnCancel = (Button) layout.findViewById(R.id.dialog_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //start MainActivity
                openActivity(MainActivity.class);
                overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
                SplashActivity.this.finish();
                dialog.dismiss();

            }
        });

       /*// 6. Show only one button to come ture Updata.
        btnCancel.setVisibility(View.GONE);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(UtilFunction.dip2px(getApplicationContext(), 120), UtilFunction.dip2px(getApplicationContext(), 48));
        btnOK.setLayoutParams(params);*/
    }


布局文件:R.layout.alert_dialog

 

<pre style="font-family: Consolas; font-size: 12pt; background-color: rgb(255, 255, 255);"><span style="font-style:italic;"><?</span><span style="color:#0000ff;"><strong>xml version=</strong></span><span style="color:#008000;"><strong>"1.0" </strong></span><span style="color:#0000ff;"><strong>encoding=</strong></span><span style="color:#008000;"><strong>"utf-8"</strong></span><span style="font-style:italic;">?>
</span><<span style="color:#000080;"><strong>RelativeLayout </strong></span><span style="color:#0000ff;"><strong>xmlns:</strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>=</strong></span><span style="color:#008000;"><strong>"http://schemas.android.com/apk/res/android"
</strong></span><span style="color:#008000;"><strong>    </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"match_parent"
</strong></span><span style="color:#008000;"><strong>    </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"wrap_content"
</strong></span><span style="color:#008000;"><strong>    </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:minWidth=</strong></span><span style="color:#008000;"><strong>"200dp"
</strong></span><span style="color:#008000;"><strong>    </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_gravity=</strong></span><span style="color:#008000;"><strong>"center"
</strong></span><span style="color:#008000;"><strong>    </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:background=</strong></span><span style="color:#008000;"><strong>"#00000000"
</strong></span><span style="color:#008000;"><strong>    </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:gravity=</strong></span><span style="color:#008000;"><strong>"center"</strong></span>>

    <<span style="color:#000080;"><strong>RelativeLayout
</strong></span><span style="color:#000080;"><strong>        </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:id=</strong></span><span style="color:#008000;"><strong>"@+id/rl_dialog_content"
</strong></span><span style="color:#008000;"><strong>        </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"match_parent"
</strong></span><span style="color:#008000;"><strong>        </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"wrap_content"
</strong></span><span style="color:#008000;"><strong>        </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginLeft=</strong></span><span style="color:#008000;"><strong>"20dp"
</strong></span><span style="color:#008000;"><strong>        </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginRight=</strong></span><span style="color:#008000;"><strong>"20dp"
</strong></span><span style="color:#008000;"><strong>        </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:background=</strong></span><span style="color:#008000;"><strong>"@drawable/shape_top_corner_no_bottom_line"
</strong></span><span style="color:#008000;"><strong>        </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:padding=</strong></span><span style="color:#008000;"><strong>"4dp"</strong></span>>


        <<span style="color:#000080;"><strong>TextView
</strong></span><span style="color:#000080;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:id=</strong></span><span style="color:#008000;"><strong>"@+id/dialog_title"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"match_parent"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"wrap_content"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginTop=</strong></span><span style="color:#008000;"><strong>"16dp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:gravity=</strong></span><span style="color:#008000;"><strong>"center"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:singleLine=</strong></span><span style="color:#008000;"><strong>"true"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:text=</strong></span><span style="color:#008000;"><strong>"</strong></span><span style="color:#008000;font-family:'宋体';"><strong>版本更新提示</strong></span><span style="color:#008000;"><strong>"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:textSize=</strong></span><span style="color:#008000;"><strong>"21sp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:textStyle=</strong></span><span style="color:#008000;"><strong>"bold" </strong></span>/>

        <<span style="color:#000080;"><strong>View
</strong></span><span style="color:#000080;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"match_parent"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"4dp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_below=</strong></span><span style="color:#008000;"><strong>"@+id/dialog_title"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginLeft=</strong></span><span style="color:#008000;"><strong>"10dp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginRight=</strong></span><span style="color:#008000;"><strong>"10dp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginTop=</strong></span><span style="color:#008000;"><strong>"10dp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:background=</strong></span><span style="color:#008000;"><strong>"@color/red" </strong></span>/>

        <<span style="color:#000080;"><strong>TextView
</strong></span><span style="color:#000080;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:id=</strong></span><span style="color:#008000;"><strong>"@+id/dialog_msg"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"match_parent"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"wrap_content"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_below=</strong></span><span style="color:#008000;"><strong>"@id/dialog_title"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginLeft=</strong></span><span style="color:#008000;"><strong>"14dp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginRight=</strong></span><span style="color:#008000;"><strong>"14dp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginTop=</strong></span><span style="color:#008000;"><strong>"20dp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:gravity=</strong></span><span style="color:#008000;"><strong>"left"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:minHeight=</strong></span><span style="color:#008000;"><strong>"100dp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:text=</strong></span><span style="color:#008000;"><strong>"</strong></span><span style="color:#008000;font-family:'宋体';"><strong>你父母知道吗</strong></span><span style="color:#008000;"><strong>?"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:textSize=</strong></span><span style="color:#008000;"><strong>"18sp" </strong></span>/>

        <<span style="color:#000080;"><strong>LinearLayout
</strong></span><span style="color:#000080;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"fill_parent"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"wrap_content"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:gravity=</strong></span><span style="color:#008000;"><strong>"center"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_below=</strong></span><span style="color:#008000;"><strong>"@id/dialog_msg"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginTop=</strong></span><span style="color:#008000;"><strong>"20dp"
</strong></span><span style="color:#008000;"><strong>            </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:orientation=</strong></span><span style="color:#008000;"><strong>"horizontal"</strong></span>>

            <<span style="color:#000080;"><strong>Button
</strong></span><span style="color:#000080;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:id=</strong></span><span style="color:#008000;"><strong>"@+id/dialog_ok"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"wrap_content"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"wrap_content"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_weight=</strong></span><span style="color:#008000;"><strong>"1"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:background=</strong></span><span style="color:#008000;"><strong>"@drawable/btn_ensure"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:text=</strong></span><span style="color:#008000;"><strong>"</strong></span><span style="color:#008000;font-family:'宋体';"><strong>确定</strong></span><span style="color:#008000;"><strong>"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:textColor=</strong></span><span style="color:#008000;"><strong>"#ffffff" </strong></span>/>

            <<span style="color:#000080;"><strong>Button
</strong></span><span style="color:#000080;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:id=</strong></span><span style="color:#008000;"><strong>"@+id/dialog_cancel"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_width=</strong></span><span style="color:#008000;"><strong>"wrap_content"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_height=</strong></span><span style="color:#008000;"><strong>"wrap_content"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_marginLeft=</strong></span><span style="color:#008000;"><strong>"5dp"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:layout_weight=</strong></span><span style="color:#008000;"><strong>"1"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:background=</strong></span><span style="color:#008000;"><strong>"@drawable/btn_cancel"
</strong></span><span style="color:#008000;"><strong>                </strong></span><span style="color:#660e7a;"><strong>android</strong></span><span style="color:#0000ff;"><strong>:text=</strong></span><span style="color:#008000;"><strong>"</strong></span><span style="color:#008000;font-family:'宋体';"><strong>取消</strong></span><span style="color:#008000;"><strong>" </strong></span>/>
        </<span style="color:#000080;"><strong>LinearLayout</strong></span>>
    </<span style="color:#000080;"><strong>RelativeLayout</strong></span>>

</<span style="color:#000080;"><strong>RelativeLayout</strong></span>>
 

8.解决问题

android studio 要签名到处APP 这是时候签名会报错误,APP_NAME" IS NOT TRANSLATED IN ZH, ZH_CN……..解决办法归根结底,这是打包的时候的问题么,所以提示说的也算清楚,就是改配置。无奈目前不提供视窗化的修改方式,所以一开始没敢乱动。解决方法如下,

打开项目目录下的build.gradle文件(这就是gradle的配置文件,gradle就是编译工具了),然后按照提示一股脑把新参数全部加到android里面。

   android {
     …
     lintOptions{
     checkReleaseBuilds false
      abortOnError false
   }
 }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值