1、下面直接给出demo,具体说明看代码注释:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void installapk(View v)
{
String path=Environment.getExternalStorageDirectory().toString();
File file=new File(path,"phone.apk");
Uri uri=Uri.fromFile(file);
// System.out.println(path);
Intent intent=new Intent();
intent.setAction(Intent.ACTION_VIEW);
// intent.setData(uri);
// //setType指定数据类型的,它会清空intent绑定的数据,因此会异常,
// 如果在前面设置type,再设置数据,则设置type无效,因此要调用下面的api,下面是setType的说明:
//This is used to create intents that only specify a type and not data,
//for example to indicate the type of data to return
intent.setType("application/vnd.android.package-archive");
//如果不设置数据类型,最终虽然安装了应用程序,但会打开浏览器
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
}
}
2、文件里APK的数据类型是在tomcat/conf/web.xml里查找的,关于MIME Type数据类型,可以在tomcat/conf/web.xml文件里搜索查找相关的数据类型,MIME Type是现在公认的,数据类型标准,浏览器也是通过MIME Type区分不同的媒体资源的。
原文:http://www.cnblogs.com/bokeofzp/p/4761306.html