注意:
intent.setdataandtype(uri.fromfile(file), "application/vnd.android.package-archive");这一句话中,第一个参数是要安装的apk的路径,第二个参数是apk所对应的类型。可以砸tomcat的安装目录下的conf目录下的web.xml中找到
程序运行截图:
代码实现如下:
1、main.xml
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="安装"
android:onclick="install"
/>
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="卸载"
android:onclick="uninstall"
/>
</linearlayout>
2、mainactivity
复制代码 代码如下:
package com.njupt.install;
import java.io.file;
import android.net.uri;
import android.os.bundle;
import android.os.environment;
import android.app.activity;
import android.content.intent;
import android.view.menu;
import android.view.view;
public class mainactivity extends activity {
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
}
public void install(view v){
intent intent = new intent();
intent.setaction(intent.action_view);
file file = new file(environment.getexternalstoragedirectory(),"htmlui1.apk");
intent.setdataandtype(uri.fromfile(file), "application/vnd.android.package-archive");
startactivity(intent);
}
public void uninstall(view v){
intent intent = new intent();
intent.setaction(intent.action_delete);
intent.setdata(uri.parse("package:com.njupt.htmlui1"));
startactivity(intent);
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.main, menu);
return true;
}
}