实例介绍
【实例简介】
【实例截图】



【核心代码】
package com.test.sample;
import java.io.File;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.iflytek.autoupdate.IFlytekUpdate;
import com.iflytek.autoupdate.IFlytekUpdateListener;
import com.iflytek.autoupdate.UpdateConstants;
import com.iflytek.autoupdate.UpdateErrorCode;
import com.iflytek.autoupdate.UpdateInfo;
import com.iflytek.autoupdate.UpdateType;
public class MainActivity extends Activity implements OnClickListener {
private Button diag;
private Button noti;
private Button deleteBtn;
Context mContext;
private IFlytekUpdate updManager;
private Toast mToast;
@SuppressLint("ShowToast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this.getApplicationContext();
diag = (Button) findViewById(R.id.diaglog);
diag.setOnClickListener(this);
noti = (Button) findViewById(R.id.noti);
noti.setOnClickListener(this);
deleteBtn = (Button) findViewById(R.id.btn_delete);
deleteBtn.setOnClickListener(this);
TextView tx = (TextView)findViewById(R.id.textView1);
try {
PackageManager manager = mContext.getPackageManager();
PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);
tx.setText("" info.versionCode);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
updManager = IFlytekUpdate.getInstance(mContext);
updManager.setDebugMode(true);
updManager.setParameter(UpdateConstants.EXTRA_WIFIONLY, "true");
// 设置通知栏icon,默认使用SDK默认
updManager.setParameter(UpdateConstants.EXTRA_NOTI_ICON, "false");
mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.diaglog:
updManager.setParameter(UpdateConstants.EXTRA_STYLE, UpdateConstants.UPDATE_UI_DIALOG);
updManager.autoUpdate(MainActivity.this, updateListener);
break;
case R.id.noti:
updManager.setParameter(UpdateConstants.EXTRA_STYLE, UpdateConstants.UPDATE_UI_NITIFICATION);
updManager.autoUpdate(MainActivity.this, updateListener);
break;
case R.id.btn_delete:
// 删除已下载的更新文件
String path = "";
if(Environment.MEDIA_MOUNTED.equalsIgnoreCase(Environment.getExternalStorageState())) {
path = Environment.getExternalStorageDirectory() "/download/iFlyUpdate";
}
if(TextUtils.isEmpty(path)) {
showTip("文件路径不正确!");
return;
}
File file = new File(path);
delFile(file);
showTip("文件已删除!");
default:
break;
}
}
private IFlytekUpdateListener updateListener = new IFlytekUpdateListener() {
@Override
public void onResult(int errorcode, UpdateInfo result) {
if(errorcode == UpdateErrorCode.OK && result!= null) {
if(result.getUpdateType() == UpdateType.NoNeed) {
showTip("已经是最新版本!");
return;
}
updManager.showUpdateInfo(MainActivity.this, result);
}
else
{
showTip("请求更新失败!\n更新错误码:" errorcode);
}
}
};
private void delFile(File deleteFile) {
if(!deleteFile.exists()) {
return;
}
if(!deleteFile.isDirectory()) {
deleteFile.delete();
} else {
File[] fileList = deleteFile.listFiles();
if(null == fileList || fileList.length<=0) {
deleteFile.delete();
return;
}
for(File file : fileList) {
delFile(file);
}
deleteFile.delete();
}
}
private void showTip(final String str) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mToast.setText(str);
mToast.show();
}
});
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论