android应用更新升级模块升级会卡引发CF-创新互联
今天在做应用升级的模块的时候,给大家分享一下升级的相关代码模块,在升级的过程前期的下载,和现在弹出的相关窗体这些的没有什么难度的,就是有个重要的地方,在每次要刷新下载进度更新的时候,要给个有条件更新,如果每次够让他跟新的,会导致应用边卡,引发CF等问题,下面是相关的代码分享,若其他问题可以私聊我!!!
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比修水网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式修水网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖修水地区。费用合理售后完善,10多年实体公司更值得信赖。本应用用到了两个开源库:
需要的人找我私聊
public class MainActivity extends Activity {
private int mCurVersion;
private ObjUpData updata;
private static NotificationManager mNm;
private static RemoteViews mrRemoteViews;
private static Notification notification;
private static int tmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 点击下载
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
updatautil();
}
});
//若点击下载后,在点别的会倒是CF
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "卡了!!我就呵呵了", Toast.LENGTH_SHORT).show();
}
});
}
public int getCurrentVersion() {
PackageInfo info = null;
try {
PackageManager pm = getPackageManager();
info = pm.getPackageInfo(getPackageName(), 0);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.e("getCurrentVersion", "info.versionCode=" + info.versionCode);
return info.versionCode;
}
private void updatautil() {
// 小模块:实现版本升级
// 打开应用
// ---->有新版本,弹出dialog(更新了xx功能,修复了xxbug,提升自动定位的准确性...);
// 如果点击取消就不更新,
// 点击确定才开始下载最新安装包
// --->下载完成,下拉点击,跳转到安装界面
// 有新版本,弹出dialog
// 获取当前版本
mCurVersion = getCurrentVersion();
// 获取新版本,下载文件,解析得到文件中所有数据
HttpUtil.post("http://192.168.1.103:8080/tins//equRgController/doDownloadNewVersion.do",//下载路径
new RequestParams(), new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String content) {
Log.e("HttpUtil.post", "statusCode=" + statusCode + "headers=" + headers + "content=" + content);
UpData data = new Gson().fromJson(content, UpData.class);
updata = data.getObj();
// 得到新版本号
int newVersion = updata.getVersion();
// 将新版本号跟就版本进行比较
if (newVersion > mCurVersion) {
// 弹出dialog提示是否需要更新并显示更新简介
showMyDialog();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable error, String content) {
}
});
}
private void showMyDialog() {
final Dialog mDialog = new Dialog(this);
mDialog.setTitle("发现新版本");
View view = getLayoutInflater().inflate(R.layout.dialog_item, null);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText(updata.getDesc());
view.findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
new MyAsyn(mNm).execute(updata.getLoadUrl());
ShowPedding();
}
});
view.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
mDialog.setContentView(view);
mDialog.show();
}
//启动异步下载
static class MyAsyn extends AsyncTask
private NotificationManager mNm;
public MyAsyn(NotificationManager mNm) {
this.mNm = mNm;
}
@Override
protected Integer doInBackground(String... params) {
int length = 0;
FileOutputStream fos = null;
try {
URL url = new URL(params[0]);
URLConnection openConnection = url.openConnection();
InputStream is = openConnection.getInputStream();
length = openConnection.getContentLength();
byte[] buffer = new byte[1024];
int end = 0;
int sum = 0;
Log.e("doInBackground", "Environment.getExternalStorageDirectory().getPath()="
+ Environment.getExternalStorageDirectory().getPath());
fos = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/TTins.apk");
// fos = new FileOutputStream("/mnt/sdcard/TTins.apk");
while (-1 != (end = is.read(buffer))) {
fos.write(buffer, 0, end);
int resent = sum * 100 / length;
sum += end;
if (resent % 6 == 0&&tmp!=resent) {
tmp=resent;
publishProgress(length, sum, resent);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return length;
}
@Override
protected void onProgressUpdate(Integer... values) {
mrRemoteViews.setProgressBar(R.id.progressBar1, values[0], values[1], false);
mrRemoteViews.setTextViewText(R.id.textView1, "已下载" + values[2] + "%");
mNm.notify(1, notification);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Integer result) {
mrRemoteViews.setProgressBar(R.id.progressBar1, result, result, false);
mrRemoteViews.setTextViewText(R.id.textView1, "下载完成");
mNm.notify(1, notification);
super.onPostExecute(result);
}
}
private void ShowPedding() {
// private static void ShowPedding(Activity activity,
// NotificationManager mNm) {
String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TTins.apk";
// 创建URI
Uri uri = Uri.fromFile(new File(fileName));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mrRemoteViews = new RemoteViews(getPackageName(), R.layout.remote_item);
notification = new NotificationCompat.Builder(this).setTicker("开始下载").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent).setContent(mrRemoteViews).build();
mNm.notify(1, (Notification) notification);
}
}
弹出dialog的XML文件
android:layout_height="match_parent" >
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#ffffff"
android:layout_alignParentTop="true" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/p_w_picpathView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="23dp"
android:layout_toLeftOf="@+id/p_w_picpathView1"
android:text="取消" />
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/p_w_picpathView1"
android:text="确定" />
下面分享一下要用的两个XML文件
android:layout_height="match_parent" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toLeftOf="@+id/button1"
android:layout_toRightOf="@+id/p_w_picpathView1" />
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/p_w_picpathView1"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
名称栏目:android应用更新升级模块升级会卡引发CF-创新互联
当前链接:http://ybzwz.com/article/cospcg.html