【达内课程】酷跑项目:检查升级_xml
【达内课程】酷跑项目:检查升级_ide_02
读取xml

res下新建xml文件夹,在里边新建config.xml,我们需要读取里边的内容,方便获取更新数据

<?xml version="1.0" encoding="utf-8"?>
<config>
<apkServerUrl>http://localhost:8080</apkServerUrl>
<baseUrl>file:///android_asset/www/</baseUrl>
</config>

MyApplication中读取config文件,获取服务器地址

public class MyApplication extends Application {
......

public static String apkServerUrl;

......

public void readConfig(){
//读取xml有3种方式,sax,dom4j,pull
//google推荐pull,占内存少
try {
//读取res/xml/config
Resources resources = getResources();
//InputStream inputStream = resources.getXml(R.xml.config);
//XmlPullParser xmlPullParser = XmlPullParserFactory.newInstance().newPullParser();
//由于getXml得到的是XmlPullParser的子类,所以以上两行代码可以合并为
XmlResourceParser xmlPullParser = resources.getXml(R.xml.config);
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT){
if(eventType == xmlPullParser.START_TAG){
String tagName = xmlPullParser.getName();
if("apkServerUrl".equals(tagName)){
apkServerUrl = xmlPullParser.nextText();
Log.d("MyApplication","apkServer:"+apkServerUrl);
}
}
eventType = xmlPullParser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}

@Override
public void onCreate() {
super.onCreate();
......

//读取config.xml
readConfig();
}
}

?关于​​pull解析xml​

联网

引入xutils3.0,并在MyApplication中初始化,不再赘述,关于xutils的使用可以戳以下链接

?关于​​xutils3.0使用​

我们的版本信息一般放在app下的build.gradle中
【达内课程】酷跑项目:检查升级_versionCode_03
versionCode :对消费者不可见,仅用于应用市场、程序内部识别版本,判断新旧等用途
versionName:展示给消费者,消费者会通过它认知自己安装的版本

增加一个工具类Tools,增加一个读取当前版本的方法

public class Tools {
/**
* 获取当前版本信息
* @param context
* @return
* @throws PackageManager.NameNotFoundException
*/
public static String getCurrentVersion(Context context) throws PackageManager.NameNotFoundException {
PackageManager packageManager = context.getPackageManager();
String packageName = context.getPackageName();
PackageInfo packageInfo = packageManager.getPackageInfo(packageName,0);
return packageInfo.versionName;
}
}

新建UpdateBiz,来获取服务器相关版本信息,获取成功后发送消息

public class UpdateBiz {
public static final String TAG = "UpdateBiz";

public void getNewVersion(final Handler handler){
String url = MyApplication.apkServerUrl+"/site/app-info?user_agent=szyapp/android";
RequestParams requestParams = new RequestParams(url);
x.http().get(requestParams, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
Log.d(TAG,"success"+result);
VersionEntity entity = null;
try {
entity = UpdateParser.parser(result);
} catch (Exception e) {
e.printStackTrace();
}finally {
//得到message
Message message = handler.obtainMessage();
//设置消息编号
message.what = MeFragment.MSG_SHOW;
//设置消息内容
//message.obj = entity;
//使用bundle好处:可以传多个数据
Bundle bundle = new Bundle();
bundle.putSerializable("versionEntity",entity);
message.setData(bundle);
//发送消息
handler.sendMessage(message);
}
}

@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.d(TAG,"error"+ex);

}

@Override
public void onCancelled(CancelledException cex) {
Log.d(TAG,"cancel");

}

@Override
public void onFinished() {
Log.d(TAG,"finish");

}
});
}
}

我们用阿里的fastjson来解析获取的数据,建实体类

VersionEntity

public class VersionEntity implements Serializable {
public int code;
public DataModel data;
public String message;


public class DataModel {
public String version;
public String app_android_update_content;
public String update_url;
}
}

新建UpdateParser,来解析json

public class UpdateParser {
public static VersionEntity parser(String json){
return JSON.parseObject(json,VersionEntity.class);
}
}

?关于​​Android中使用Fastjson​

MeFragment接受发送的通知,做相关处理,MeFragment的布局是fragment_me,增加一个检查更新按钮,添加点击事件

public class MeFragment extends Fragment {
Button extButton;
Button updateButton;

public static final String TAG = "MeFragment";

//biz发送通知 通知MeFragment显示版本
public static final int MSG_SHOW = 1;
//biz发送通知 通知MeFragment安装apk
public static final int MSG_INSTALL = 2;
public static final int MSG_ERROR = 3;

Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
int msgId = msg.what;
Bundle bundle = msg.getData();
final VersionEntity versionEntity = (VersionEntity)bundle.getSerializable("versionEntity");
switch (msgId){
case MSG_SHOW:
//判断服务器上的版本是不是最新的
String currentVersion = null;
try {
currentVersion = Tools.getCurrentVersion(getActivity());
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
String serverApkVersion = versionEntity.data.version;
if(Double.parseDouble(serverApkVersion)>Double.parseDouble(currentVersion)){
//有最新的应用,显示Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(versionEntity.data.version+"\n"+versionEntity.data.app_android_update_content);

builder.setPositiveButton("升级", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.d(TAG,"升级地址:"+versionEntity.data.update_url);
}
});
builder.setNegativeButton("取消",null);
builder.show();
}else {
Toast.makeText(getActivity(),"当前为最新应用,无升级",Toast.LENGTH_SHORT).show();
}
//显示dialog
//
break;
case MSG_INSTALL:
break;
case MSG_ERROR:
break;
}
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_me, null);
initView(view);
setListener();
return view;
}

private void setListener() {
MyListener listener = new MyListener();
extButton.setOnClickListener(listener);
UpdateListener updateListener = new UpdateListener();
updateButton.setOnClickListener(updateListener);
}

private void initView(View view) {
extButton = view.findViewById(R.id.btn_me_exit);
updateButton = view.findViewById(R.id.btn_me_update);
}

class MyListener implements View.OnClickListener {
@Override
public void onClick(View view) {
MainFragmentActivity activity = (MainFragmentActivity) getActivity();
MyApplication application = (MyApplication) activity.getApplication();
application.finish();
}
}

class UpdateListener implements View.OnClickListener {
@Override
public void onClick(View view) {
UpdateBiz updateBiz = new UpdateBiz();
updateBiz.getNewVersion(handler);
}
}
}