文章转自:http://blog.youkuaiyun.com/fenghome/archive/2010/06/16/5673757.aspx
在Android安装卸载程序的源码中我们知道:
- <activityandroid:name=".PackageInstallerActivity">
- <intent-filter>
- <actionandroid:name="android.intent.action.VIEW"/>
- <categoryandroid:name="android.intent.category.DEFAULT"/>
- <dataandroid:scheme="content"/>
- <dataandroid:scheme="file"/>
- <dataandroid:mimeType="application/vnd.android.package-archive"/>
- </intent-filter>
- </activity>
- <activityandroid:name=".UninstallerActivity">
- <intent-filter>
- <actionandroid:name="android.intent.action.VIEW"/>
- <actionandroid:name="android.intent.action.DELETE"/>
- <categoryandroid:name="android.intent.category.DEFAULT"/>
- <dataandroid:scheme="package"/>
- </intent-filter>
- </activity>
因为根据里面的权限我们可以 从sd卡安装一个程序:
- StringfileName=Environment.getExternalStorageDirectory()+"/myApp.apk";
- Intentintent=newIntent(Intent.ACTION_VIEW);
- intent.setDataAndType(Uri.parse("file://"+filePath),"application/vnd.android.package-archive");
- //或者
- //intent.setDataAndType(Uri.fromFile(newFile(fileName)),"application/vnd.android.package-archive");
- startActivity(intent);
Android安装卸载程序的操作中要想卸载一个程序:
- UripackageURI=Uri.parse("package:com.android.myapp");
- IntentuninstallIntent=newIntent(Intent.ACTION_DELETE,packageURI);
- startActivity(uninstallIntent);
默认是不支持安装非市场程序的 因此判断一下
- intresult=Settings.Secure.getInt(getContentResolver(),Settings.Secure.INSTALL_NON_MARKET_APPS,0);
- if(result==0){
- //showsomedialoghere
- //...
- //andmaybeshowapplicationsettingsdialogmanually
- Intentintent=newIntent();
- intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
- startActivity(intent);
- }
<wbr style="line-height:25px"></wbr>