Created
December 11, 2012 12:37
-
-
Save myamamic/4258271 to your computer and use it in GitHub Desktop.
[Java][Android] アプリからAPKをインストール/アンインストールするクラス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 動作確認:Android 2.2系 OK | |
* 2.3系 未確認 | |
* 4.0系 未確認 | |
* | |
* TODO | |
* インストール完了待ちとアンインストール完了待ちのタイムアウトが | |
* "infinite"なので、時間で区切りたい。 | |
*/ | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.pm.ApplicationInfo; | |
import android.content.pm.PackageManager; | |
import android.content.pm.PackageManager.NameNotFoundException; | |
import android.net.Uri; | |
import java.io.File; | |
public class ApkManager { | |
private final static String TAG = "ApkManager"; | |
private PackageManager mPm = null; | |
private Activity mCurrentActivity = null; | |
public ApkManager(PackageManager pm, Activity currentActivity) { | |
mPm = pm; | |
mCurrentActivity = currentActivity; | |
} | |
/** | |
* @param packageName Full package name. | |
* @return If exist the apk return true, otherwise false. | |
*/ | |
public boolean IsExistApk(String packageName) { | |
try { | |
mPm.getApplicationInfo(packageName, 0); | |
// Package is already installed. | |
return true; | |
} catch(NameNotFoundException nnfe) { | |
// Package is not installed. | |
return false; | |
} | |
} | |
/** | |
* @param apkFullPath Full path to install apk. | |
* @param packageName Full package name. | |
* @return If success return true, otherwise false. | |
* @see This api call startActivity() and wait completion of install activity. | |
*/ | |
public boolean InstallApkAndWaitComplete(String apkFullPath, String packageName) { | |
if ((apkFullPath == null)||(apkFullPath.length() <= 0) | |
||(packageName == null)||(packageName.length() <= 0)) { | |
return false; | |
} | |
InstallApk(apkFullPath); | |
boolean isFind = false; | |
while (isFind != true) { // TODO: Set timeout | |
try { | |
final ApplicationInfo ai = mPm.getApplicationInfo(packageName, 0); | |
isFind = true; | |
} catch(NameNotFoundException nnfe) { | |
// continue | |
} | |
} | |
return true; | |
} | |
/** | |
* @param packageName Full package name. | |
* @return If success return true, otherwise false. | |
* @see This api call startActivity() and wait completion of uninstall activity. | |
*/ | |
public boolean UninstallApkAndWaitComplete(String packageName) { | |
if ((packageName == null)||(packageName.length() <= 0)) { | |
return false; | |
} | |
UninstallApk(packageName); | |
boolean isFind = true; | |
while (isFind != false) { // TODO: Set timeout | |
try { | |
final ApplicationInfo ai = mPm.getApplicationInfo(packageName, 0); | |
isFind = true; | |
continue; | |
} catch(NameNotFoundException nnfe) { | |
// Uninstall completed. | |
break; | |
} | |
} | |
return true; | |
} | |
/** | |
* @param apkFullPath Full path to install apk. | |
* @return If success return true, otherwise false. | |
* @see This api only call startActivity() and start install activity. | |
* It doesn't wait install completion. | |
*/ | |
public boolean InstallApk(String apkFullPath) { | |
if ((apkFullPath == null)||(apkFullPath.length() <= 0)) { | |
return false; | |
} | |
Intent intent = new Intent(Intent.ACTION_VIEW); | |
intent.setDataAndType(Uri.fromFile(new File(apkFullPath)), "application/vnd.android.package-archive" ); | |
mCurrentActivity.startActivity(intent); | |
return true; | |
} | |
/** | |
* @param packageName Full package name. | |
* @return If success return true, otherwise false. | |
* @see This api only call startActivity() and start uninstall activity. | |
* It doesn't wait uninstall completion. | |
*/ | |
public boolean UninstallApk(String packageName) { | |
if ((packageName == null)||(packageName.length() <= 0)) { | |
return false; | |
} | |
Uri uri = Uri.fromParts("package", packageName, null); | |
Intent intent = new Intent(Intent.ACTION_DELETE, uri); | |
mCurrentActivity.startActivity(intent); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
作ったのが古いので、Android 4.x系で動作するかわかりません
TODO
4.x系での動作確認