public class PackageManager {
private Context mContext;
private String apkName = "";
public PackageManager(Context mContext, String apkName) {
// TODO Auto-generated constructor stub
this.mContext = mContext;
this.apkName = apkName;
}
* 如xxx.apk
*/
public void installApk(/*InputStream is*/) {
String apkPathName =Environment.getExternalStorageDirectory()+ "/kuaixindai/"+ apkName;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String type = "application/vnd.android.package-archive";
try {
// //不知道什么原因,当apk大于5M的时候就读取异常
File f = new File(apkPathName);
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = mContext.openFileOutput(apkName,
Context.MODE_PRIVATE + Context.MODE_WORLD_READABLE);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
File f = new File(apkPathName);
if (f.exists()) {
intent.setDataAndType(Uri.fromFile(f), type);
mContext.startActivity(intent);
}
}
//卸载
public void uninstallApk(String packageName) {
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
mContext.startActivity(uninstallIntent);
}
private Context mContext;
private String apkName = "";
public PackageManager(Context mContext, String apkName) {
// TODO Auto-generated constructor stub
this.mContext = mContext;
this.apkName = apkName;
}
/**安装
* @param apkName* 如xxx.apk
*/
public void installApk(/*InputStream is*/) {
String apkPathName =Environment.getExternalStorageDirectory()+ "/kuaixindai/"+ apkName;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String type = "application/vnd.android.package-archive";
try {
// //不知道什么原因,当apk大于5M的时候就读取异常
File f = new File(apkPathName);
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = mContext.openFileOutput(apkName,
Context.MODE_PRIVATE + Context.MODE_WORLD_READABLE);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
File f = new File(apkPathName);
if (f.exists()) {
intent.setDataAndType(Uri.fromFile(f), type);
mContext.startActivity(intent);
}
}
//卸载
public void uninstallApk(String packageName) {
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
mContext.startActivity(uninstallIntent);
}