大家好,我是segmentfault的新人,给大家带来一个做了一个星期的SDK,可以实现将Android APP碎片化管理,自由更新,实时更新,低流量耗费的更新~~
Zag Whim Renewal
A system for quicker updating on Android
首先你要有一个Bmob移动后端云服务平台的账号
接下来,进入APP的数据浏览,在APP内建ZwrFile表,添加File类型的file列,添加String类型的name列
在Eclipse或者Android studio新建两个项目,称为Basic端和Expand端,分别导入对应的SDK
Basic端的Activity继承自ZwrActivity,实现InitListener接口,在onCreate方法内,先super,再Bmob.initialize(getApplicationContext(), appid),最后setZwrView("类名", this, true, false);
appid取自bmob后台,在“应用信息”里查看
“类名”为Expand端对应的类名,如“com.test.bmob.FirstView”
编译运行Basic端到手机
放一个layout.xml文件到ZwrFile表的file,name可以随便取,比如“main.xml”
Expand端删除所有代码,新建一个类,类名为上文所提到的(“com.test.bmob.FirstView”),继承自ZwrExpandView
将实现的抽象方法的LayoutName返回layout文件在ZwrFile对应的name,如上文的“main.xml”
在init函数内写初始化方法
编译Expand端,将编译出来的bin文件夹下的classes.dex上传到ZwrFile,name改为类名(“com.test.bmob.FirstView”)
启动Basic端,即可看到更新效果
以后需要更新逻辑代码,只需更新Activity对应的dex文件即可,需要更新布局,就更新对应的xml文件
部分代码示例:
Basic端:
import android.os.Bundle;
import android.widget.Button;
import cn.bmob.v3.Bmob;
import com.zwr.ZwrActivity;
import com.zwr.ZwrListener.InitListener;
public class MainActivity extends ZwrActivity implements InitListener {
private Button bt;
private final String appid = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bmob.initialize(getApplicationContext(),appid);
setZwrView("com.testzwr.ThirdView", this, true);
}
@Override
public void succeed() {
init();
}
@Override
public void fail(String why) {
toast(why);
}
@Override
public void lastView() {
init();
}
private final void init() {
bt = getView("zwrButton");
if (bt != null) {
bt.setText("hahhaa");
}
}
}
Extand端:
package com.testzwr;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.zwr.ZwrBasic.ZwrExpandView;
import com.zwr.ZwrTool;
public class ThirdView extends ZwrExpandView implements OnClickListener {
private Button bt;
private EditText et;
private TextView tv;
private Button bt_toast, bt_dialog, bt_change;
public ThirdView(Activity activity) {
super(activity);
}
@Override
public String LayoutName() {
return "third_layout.xml";
}
@Override
public void init() {
try {
et = getView("msg_et");
bt = getView("msg_send");
tv = getView("msg_show");
bt_toast = getView("toast");
bt_dialog = getView("dialog");
bt_change = getView("change");
} catch (NullPointerException e) {
return;
}
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.append(et.getText().toString() + "\n");
et.setText("");
}
});
bt_toast.setOnClickListener(this);
bt_dialog.setOnClickListener(this);
bt_change.setOnClickListener(this);
}
private AlertDialog dialog;
@Override
public void onClick(View v) {
int i = v.getId();
if (i == bt_toast.getId()) {
Toast.makeText(getContext(), "hahaha", 1).show();
} else if (i == bt_dialog.getId()) {
if (dialog == null)
dialog = new AlertDialog.Builder(getContext())
.setMessage("Hi~~").setTitle("恭喜你!").create();
dialog.show();
} else if (i == bt_change.getId()) {
bt_toast.setText("改变啦~丧失Toast能力,但能跳转到另一个Activity");
bt_dialog.setText("改变了还能弹窗");
bt_toast.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
ZwrTool.startActivity("com.test.sdkbuilder.SecondActivity");
} catch (ClassNotFoundException e) {
Toast.makeText(getContext(), "no this class", 1).show();
e.printStackTrace();
}
}
});
v.setVisibility(View.GONE);
}
}
}
layout文件:(third_layout.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="match_parent"
android:orientation="vertical"
android:paddingBottom="10dip"
android:paddingTop="10dip" >
<Button
android:id="@+id/toast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击我就会弹吐司" />
<Button
android:id="@+id/dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击我就会弹Dialog" />
<Button
android:id="@+id/change"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点击我会改变上面两个按钮" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/msg_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<EditText
android:id="@+id/msg_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:hint="输入信息" />
<Button
android:id="@+id/msg_send"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="发送" />
</LinearLayout>
<TextView
android:id="@+id/msg_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="hahaha"
android:layout_above="@+id/msg_ll"
android:layout_alignParentTop="true" />
</RelativeLayout>
</LinearLayout>
具体说明文档及SDK下载,请下载~~
下载地址: