// 获得 下载后的 apk 版本信息
1.public class test4 extends Activity {
2.
3. @Override
4. public void onCreate(Bundle savedInstanceState) {
5. super.onCreate(savedInstanceState);
6. setContentView(R.layout.main);
7. String archiveFilePath="sdcard/download/Law.apk";//安装包路径
8. PackageManager pm = getPackageManager();
9. PackageInfo info = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);
10. if(info != null){
11. ApplicationInfo appInfo = info.applicationInfo;
12. String appName = pm.getApplicationLabel(appInfo).toString();
13. String packageName = appInfo.packageName; //得到安装包名称
14. String version=info.versionName; //得到版本信息
15. Toast.makeText(test4.this, "packageName:"+packageName+";version:"+version, Toast.LENGTH_LONG).show();
16. Drawable icon = pm.getApplicationIcon(appInfo);//得到图标信息
17. TextView tv = (TextView)findViewById(R.id.tv); //显示图标
18. tv.setBackgroundDrawable(icon);
19. }
20. }
21.
22.}
//发送自身的版本信息,来决定是否升级
/**
* 检查是否需要升级
*
*
*/
public void updateSys(){
/*
* <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tangkou.mm.nn.ui" android:versionCode="1"
android:versionName="1.0">
*
*/
String pName = "com.tangkou.mm.nn.ui";//mannifest中package内容
String versionName = null;
try {
PackageInfo pinfo = getPackageManager().getPackageInfo(pName, PackageManager.GET_CONFIGURATIONS);
versionName = pinfo.versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MGRControlCenter mcc = MGRControlCenter.getInstance();
mcc.update(versionName, new CallbackUI() {
@Override
public void execute(int errorCode, Object obj) {
// TODO Auto-generated method stub
VersionInfo versionInfo = (VersionInfo)obj;
String url = versionInfo.URL;
int code = Integer.valueOf(versionInfo.needUpdate);
switch (code) {
case 0 : //不需要升级
break;
case 1 : // 需要升级后才能使用
showUpdateDialog(1, url);
break;
case 2 : //需要升级,不升级也可以使用
showUpdateDialog(2, url);
break;
}
}
});
}
/**
* 版本升级对话框
*
*/
public void showUpdateDialog(int i, final String url){
if( i == 2){
Dialog dialog = new AlertDialog.Builder(this)
.setTitle(R.string.sysUpdate_title).setMessage(R.string.sysUpdate_message)// 设置内容
.setPositiveButton(R.string.sysUpdate_pButton,// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
pBar = new ProgressDialog(UIMainMenuActivity.this);
pBar.setTitle(R.string.sysUpdate_pBar_title);
Resources resources = getBaseContext().getResources();
String message = resources.getString(R.string.sysUpdate_pBar_message);
pBar.setMessage(message);
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile(url);
}
}).setNegativeButton(R.string.sysUpdate_nButton,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击”取消”按钮之后退出程序
}
}).create();// 创建
// 显示对话框
dialog.show();
}else if(i == 1){
Dialog dialog = new AlertDialog.Builder(this)
.setTitle(R.string.sysUpdate_title).setMessage(R.string.sysUpdate_message)// 设置内容
.setPositiveButton(R.string.sysUpdate_pButton,// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
pBar = new ProgressDialog(UIMainMenuActivity.this);
pBar.setTitle(R.string.sysUpdate_pBar_title);
pBar.setMessage(""+R.string.sysUpdate_pBar_message);
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile(url);
}
}).create();// 创建
// 显示对话框
dialog.show();
}
}
/**
* 下载安装文件
*
* @param url
*/
public void downFile(final String url) {
appName = url.substring(url.lastIndexOf("/"), url.length());
sdPath = Environment.getExternalStorageDirectory();
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
// params[0]代表连接的url
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(sdPath, appName);
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
// baos.write(buf, 0, ch);
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
/**
* 下载完后取消progressBar效果
* @
*/
public void down() {
handler.post(new Runnable() {
public void run() {
pBar.cancel();
update();
}
});
}
/**
* 启动系统更新
*
*/
public void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(sdPath.getPath()+"/"+appName)),
"application/vnd.android.package-archive");
startActivity(intent);
}