android 로더 작성
프로그래밍 2010. 11. 11. 14:41 |protected String downloadApkFile(String url) {
//
String fileName = null;
URLConnection sourceUrl;
try {
//Connect to URL and gets content
sourceUrl = new URL(url).openConnection();
Object data = sourceUrl.getContent();
//find Filename
fileName = sourceUrl.toString();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
// create/open file in the 'data/data/<app namespace>/files'
// directory
FileOutputStream fos = openFileOutput(fileName,
Context.MODE_WORLD_READABLE);
byte[] buffer = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(
(InputStream) data);
int len1 = 0;
while ((len1 = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
bis.close();
} catch (MalformedURLException e) {
Log.i("ghi", e.getMessage());
} catch (IOException e) {
Log.i("ghi", e.getMessage());
}
return getFileStreamPath(fileName).getAbsolutePath();
}
File apkFile = new File(downloadApkFile("http://new.apk"));
Intent installIntent = new Intent(Intent.ACTION_VIEW);
installIntent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
startActivity(installIntent);
// DELETE
Uri packageURI = Uri.parse("package:yourapkfullpackagename");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);