SSS
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSS相关的知识,希望对你有一定的参考价值。
package com.bwei.activity;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.bwei.activity.UpdateEntity;
import com.bwei.activity.UpdateTools;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class GuanYuActivity extends Activity {
private TextView jianc;
private AlertDialog dialog;
private Button back;
private UpdateEntity updateEntity;
// private TextView tv_name;
/**
* 消息机制
*/
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 0:
dialog.setMessage("您会更新对吗");
dialog.show();
break;
case 1:
downLoadApk();
break;
}
}
private void downLoadApk() {
final ProgressDialog pd; // 进度条对话框
pd = new ProgressDialog(GuanYuActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("正在下载更新");
pd.show();
new Thread() {
@Override
public void run() {
try {
File file = getFileFromServer(updateEntity.getUrl(), pd);
sleep(3000);
UpdateTools tools = new UpdateTools();
// 安装APk
tools.installApk(file, GuanYuActivity.this);
pd.dismiss(); // 结束掉进度条对话框
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
};
private DialogInterface.OnClickListener onclick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_guan_yu);
jianc = (TextView) findViewById(R.id.jianc);
back = (Button) findViewById(R.id.back);
// 版本更新
jianchua();
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
// 版本更新
private void jianchua() {
jianc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(GuanYuActivity.this, "更新了小爷给糖吃",0).show();
dialog = new AlertDialog.Builder(GuanYuActivity.this)
.setTitle("更新提醒").setIcon(R.drawable.logo_qq)
.setPositiveButton("在线更新", onclick)
.setNegativeButton("不想更新", null).create();
// 开启线程
new Thread(new CheckVersionTask()).start(); // 检查版本号
}
});
onclick = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.sendEmptyMessage(1);
}
};
}
/*
* 从服务器获取xml解析并进行比对版本号
*/
public class CheckVersionTask implements Runnable {
public void run() {
try {
// 从资源文件获取服务器 地址
String path = getResources().getString(R.string.serverurl);
// 包装成url的对象
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000);
InputStream is = conn.getInputStream();
updateEntity = UpdateTools.getUpdataInfo(is);
// 当前版本号
int versionCode = GuanYuActivity.this
.getPackageManager()
.getPackageInfo(GuanYuActivity.this.getPackageName(), 0).versionCode;
if (Integer.parseInt(updateEntity.getVersioncode()) <= versionCode) {
Log.i("xxx", "版本号相同无需升级");
} else {
Log.i("xxxx", "版本号不同 ,提示用户升级 ");
handler.sendEmptyMessage(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 下载方法
*
* @param path
* @param pd
* @return
* @throws Exception
*/
public File getFileFromServer(String path, ProgressDialog pd)
throws Exception {
// 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
// 获取到文件的大小
pd.setMax(conn.getContentLength());
InputStream is = conn.getInputStream();
File file = new File(Environment.getExternalStorageDirectory(),
"updata.apk");
FileOutputStream fos = new FileOutputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buffer = new byte[1024];
int len;
int total = 0;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
total += len;
// 获取当前下载量
pd.setProgress(total);
}
fos.close();
bis.close();
is.close();
return file;
} else {
return null;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
// StatService.onPause(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// StatService.onResume(this);
}
}
以上是关于SSS的主要内容,如果未能解决你的问题,请参考以下文章
SSS1530用于TYPE-C耳机 Lingting耳机方案 线控耳机方案
SSS1530用于TYPE-C耳机 Lingting耳机方案 线控耳机方案