よく使われるSDカードの操作方法、APk管理などの方法
21706 ワード
Android起動リストの取得
自己起動アプリケーションの取得
たんいへんかん
SDカード情報の取得
しんとうじょうたいらん
現在のネットワークが使用可能かどうかを確認
APKのインストール
あるプログラムが携帯電話にインストールされているかどうかを判断する
APKがインストールされているかどうかを判断
現在のバージョンのバージョン名を取得
現在のバージョンのバージョン番号の取得
WIFIが接続されているかどうかを判断する
個々のファイルを削除
ファイルのスキャン
再帰的な考え方を用いて,各ディレクトリの下のapkファイルを再帰的に探す.
すべてのアプリケーション情報を取得
/**
* Android
*/
private static final String RECEIVE_BOOT_COMPLETED = "android.permission.RECEIVE_BOOT_COMPLETED";
private static final String ACTION_BOOT_COMPLETED = "android.permission.ACTION_BOOT_COMPLETED";
public static List fetchInstalledApps(Context mContext) {
PackageManager pm = mContext.getPackageManager();
List appInfo = pm.getInstalledApplications(0);
Iterator appInfoIterator = appInfo.iterator();
List appList = new ArrayList(
appInfo.size());
while (appInfoIterator.hasNext()) {
ApplicationInfo app = appInfoIterator.next();
int flag = pm.checkPermission(RECEIVE_BOOT_COMPLETED,
app.packageName);
if (flag == PackageManager.PERMISSION_GRANTED) {
AutoStartInfo appMap = new AutoStartInfo();
String label = pm.getApplicationLabel(app).toString();
Drawable icon = pm.getApplicationIcon(app);
String packageName = app.packageName;
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
appMap.setSystem(true);
// abAppProcessInfo.isSystem = true;
} else {
appMap.setSystem(false);
// abAppProcessInfo.isSystem = false;
}
// appMap.setDesc(desc);
appMap.setIcon(icon);
appMap.setPackageName(packageName);
appMap.setLabel(label);
appList.add(appMap);
}
}
return appList;
}
自己起動アプリケーションの取得
/**
*
*
* @param mContext
* @return
*/
public static List fetchAutoApps(Context mContext) {
PackageManager pm = mContext.getPackageManager();
Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED);
List resolveInfoList = pm.queryBroadcastReceivers(intent,
PackageManager.GET_DISABLED_COMPONENTS);
List appList = new ArrayList();
//
String appName = null;
String packageReceiver = null;
Drawable icon = null;
boolean isSystem = false;
boolean isenable = true;
String packageName = null;
boolean isAutoStart = false;
boolean isBackStart = false;
/**
* PackageInfo :
*
* :packageInfo.packageName
* icon :packageManager.getApplicationIcon(applicationInfo)
* :packageManager.getApplicationLabel(applicationInfo)
* :packageManager.getPackageInfo(packageName,PackageManager.
* GET_PERMISSIONS) .requestedPermissions
*
* ResolveInfo :
*
* :resolve.activityInfo.packageName
* icon :resolve.loadIcon(packageManager)
* :resolve.loadLabel(packageManager).toString()
*
*/
if (resolveInfoList.size() > 0) {
for (int i = 0; resolveInfoList.size() > i; i++) {
isAutoStart = false;
isBackStart = false;
/**
* // package if(PackageManager.PERMISSION_GRANTED
* == context
* .getPackageManager().checkPermission(BOOT_START_PERMISSION,
* app.packageName))
*
* BOOT_COMPLETED BOOT_START_PERMISSION RECEIVE_BOOT_COMPLETED
* ACTION_BOOT_COMPLETED
*/
if (mContext.getPackageManager().checkPermission(
RECEIVE_BOOT_COMPLETED,
resolveInfoList.get(i).activityInfo.packageName) == PackageManager.PERMISSION_GRANTED) {
isAutoStart = true;
}
if (mContext.getPackageManager().checkPermission(
ACTION_BOOT_COMPLETED,
resolveInfoList.get(i).activityInfo.packageName) == PackageManager.PERMISSION_GRANTED) {
isBackStart = true;
}
appName = resolveInfoList.get(i).loadLabel(pm).toString();
packageName = resolveInfoList.get(i).activityInfo.packageName;
packageReceiver = resolveInfoList.get(i).activityInfo.packageName
+ "/" + resolveInfoList.get(i).activityInfo.name;
icon = resolveInfoList.get(i).loadIcon(pm);
ComponentName mComponentName2 = new ComponentName(
resolveInfoList.get(i).activityInfo.packageName,
resolveInfoList.get(i).activityInfo.name);
if (pm.getComponentEnabledSetting(mComponentName2) == 2) {
isenable = false;
} else {
isenable = true;
}
if ((resolveInfoList.get(i).activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
isSystem = true;
} else {
isSystem = false;
}
//
AutoStartInfo mAutoStartInfo = new AutoStartInfo();
mAutoStartInfo.setLabel(appName);
mAutoStartInfo.setSystem(isSystem);
mAutoStartInfo.setEnable(isenable);
mAutoStartInfo.setIcon(icon);
mAutoStartInfo.setPackageName(packageName);
mAutoStartInfo.setPackageReceiver(packageReceiver);
mAutoStartInfo.setAutoStart(isAutoStart);
mAutoStartInfo.setBackStart(isBackStart);
boolean isAdd = true;
if (appList != null) {
for (int j = 0; appList.size() > j; j++) {
if (appList.get(j).getPackageName().equals(packageName)) {
isAdd = false;
}
}
}
if (isAdd) {
appList.add(mAutoStartInfo);
}
}
}
return appList;
}
たんいへんかん
/**
*
*
* @param length
* @return
*/
public static String toSize(double length) {
long kb = 1024;
long mb = 1024 * kb;
long gb = 1024 * mb;
if (length < kb) {
return String.format("%d B", (int) length);
} else if (length < mb) {
return String.format("%.2f KB", length / kb);
} else if (length < gb) {
return String.format("%.2f MB", length / mb);
} else {
return String.format("%.2f GB", length / gb);
}
}
// storage, G M K B
public static String convertStorage(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb) {
return String.format("%.1f GB", (float) size / gb);
} else if (size >= mb) {
float f = (float) size / mb;
return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
} else if (size >= kb) {
float f = (float) size / kb;
return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
} else
return String.format("%d B", size);
}
public static StorageSize convertStorageSize(long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
StorageSize sto = new StorageSize();
if (size >= gb) {
sto.suffix = "GB";
sto.value = (float) size / gb;
return sto;
} else if (size >= mb) {
sto.suffix = "MB";
sto.value = (float) size / mb;
return sto;
} else if (size >= kb) {
sto.suffix = "KB";
sto.value = (float) size / kb;
return sto;
} else {
sto.suffix = "B";
sto.value = (float) size;
return sto;
}
}
SDカード情報の取得
public static SDCardInfo getSDCardInfo() {
// String sDcString = Environment.getExternalStorageState();
if (Environment.isExternalStorageRemovable()) {
String sDcString = Environment.getExternalStorageState();
if (sDcString.equals(Environment.MEDIA_MOUNTED)) {
File pathFile = Environment.getExternalStorageDirectory();
try {
StatFs statfs = new StatFs(pathFile.getPath());
// SDCard BLOCK
long nTotalBlocks = statfs.getBlockCount();
// SDCard block SIZE
long nBlocSize = statfs.getBlockSize();
// Block
long nAvailaBlock = statfs.getAvailableBlocks();
// Block ( )
long nFreeBlock = statfs.getFreeBlocks();
SDCardInfo info = new SDCardInfo();
// SDCard MB
info.total = nTotalBlocks * nBlocSize;
// SDCard MB
info.free = nAvailaBlock * nBlocSize;
return info;
} catch (IllegalArgumentException e) {
}
}
}
return null;
}
/**
* data getDataDirectory()
*
* @param context
* @return
*/
public static SDCardInfo getSystemSpaceInfo(Context context) {
File path = Environment.getDataDirectory();
// File path = context.getCacheDir().getAbsoluteFile();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long availableBlocks = stat.getAvailableBlocks();
long totalSize = blockSize * totalBlocks;
long availSize = availableBlocks * blockSize;
SDCardInfo info = new SDCardInfo();
info.total = totalSize;
info.free = availSize;
return info;
}
public static SDCardInfo getRootSpaceInfo() {
File path = Environment.getRootDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long availableBlocks = stat.getAvailableBlocks();
long totalSize = blockSize * totalBlocks;
long availSize = availableBlocks * blockSize;
// SDCard block SIZE
long nBlocSize = stat.getBlockSize();
SDCardInfo info = new SDCardInfo();
// SDCard MB
info.total = totalSize;
// SDCard MB
info.free = availSize;
return info;
}
しんとうじょうたいらん
public void sethah(Window window) {
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
//
// getWindow.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
現在のネットワークが使用可能かどうかを確認
/**
*
*
* @param activity
* @return
*/
public boolean isNetworkAvailable(Activity activity) {
Context context = activity.getApplicationContext();
// ( Wifi,net )
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
} else {
// NetworkInfo
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0) {
for (int i = 0; i < networkInfo.length; i++) {
System.out.println(i + "===state==="
+ networkInfo[i].getState());
System.out.println(i + "===style==="
+ networkInfo[i].getTypeName());
//
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
APKのインストール
/**
* APK
*
* @param context
* @param packageName
*/
public void update(Context context, String packageName) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), packageName)),
"application/vnd.android.package-archive");
context.startActivity(intent);
}
あるプログラムが携帯電話にインストールされているかどうかを判断する
/**
*
*
* @param context
*
* @param packageName
*
* @return
*/
public static boolean isPackageExist(Context context, String packageName) {
if (packageName == null)
return false;
boolean packageExist = false;
PackageManager packageManager = context.getPackageManager();
try {
packageManager.getPackageInfo(packageName, 0);
packageExist = true;
} catch (PackageManager.NameNotFoundException ignored) {
// L.d("isPackageExist:" + ignored);
}
return packageExist;
}
APKがインストールされているかどうかを判断
/**
* APK
*
* @param pm
* @param packageName
* @param versionCode
* @return
*/
public int doType(PackageManager pm, String packageName, int versionCode) {
List pakageinfos = pm
.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for (PackageInfo pi : pakageinfos) {
String pi_packageName = pi.packageName;
int pi_versionCode = pi.versionCode;
//
if (packageName.endsWith(pi_packageName)) {
// Log.i("test"," ");
if (versionCode == pi_versionCode) {
Log.i("test", " , , ");
return 1000;
} else if (versionCode > pi_versionCode) {
Log.i("test", " , ");
return 2000;
}
}
}
Log.i("test", " , ");
return 3000;
}
現在のバージョンのバージョン名を取得
/**
*
*
* @param context
* @return
*/
public String getVersion(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionName;
} catch (Exception e) {
e.printStackTrace();
return context.getResources()
.getString(R.string.unknown_versionnum);
}
}
現在のバージョンのバージョン番号の取得
/**
*
*
* @return
*/
public int getVersiontheCode(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
return packageInfo.versionCode;
} catch (Exception e) {
e.printStackTrace();
return R.string.unknown_versionnum;
}
}
WIFIが接続されているかどうかを判断する
/ **
* WIFI
*
* @param context
* @return true
*/
boolean isWifi = false;
public static boolean isWifiConnected(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiNetworkInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiNetworkInfo.isConnected()) {
return true;
}
return false;
}
個々のファイルを削除
` /**
*
*
* @param filePath
*
* @return true, false
*/
public boolean deleteFile(String filePath, Context context) {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
boolean isOK = file.delete();
scanFile(file, context);
return isOK;
} else {
Toast.makeText(context, "null", 0).show();
}
return false;
}`
ファイルのスキャン
/**
*
*
* @param file
* @param context
*/
public void scanFile(File file, Context context) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);
}
/**
* get,set
*/
private List myFiles = new ArrayList();
public List getMyFiles() {
return myFiles;
}
public void setMyFiles(List myFiles) {
this.myFiles = myFiles;
}
再帰的な考え方を用いて,各ディレクトリの下のapkファイルを再帰的に探す.
/**
* , apk
*
* @param file
*/
public void FindAllAPKFile(File file, Context context) {
// , SD APK
// file = Environment.getDataDirectory();
// SD
if (file.isFile()) {
String name_s = file.getName();
APKInfo myFile = new APKInfo();
String apk_path = null;
// MimeTypeMap.getSingleton()
if (name_s.toLowerCase().endsWith(".apk")) {
apk_path = file.getAbsolutePath();// apk
// System.out.println("----" + file.getAbsolutePath() + "" +
// name_s);
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageArchiveInfo(apk_path,
PackageManager.GET_ACTIVITIES);
ApplicationInfo appInfo = packageInfo.applicationInfo;
/** apk */
appInfo.sourceDir = apk_path;
appInfo.publicSourceDir = apk_path;
Drawable apk_icon = appInfo.loadIcon(pm);
myFile.setIcon(apk_icon);
/** */
String packageName = packageInfo.packageName;
myFile.setName(packageName);
/** apk */
myFile.setPath(file.getAbsolutePath());
/** apk String */
String versionName = packageInfo.versionName;
myFile.setVersionName(versionName);
/** apk int */
int versionCode = packageInfo.versionCode;
myFile.setVersionCode(versionCode);
/** */
int type = doType(pm, packageName, versionCode);
myFile.setType(type);
Log.i("ok", " :" + String.valueOf(type) + "
"
+ "------------------ -------------------");
myFiles.add(myFile);
}
// String apk_app = name_s.substring(name_s.lastIndexOf("."));
} else {
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File file_str : files) {
FindAllAPKFile(file_str, context);
}
}
}
}
すべてのアプリケーション情報を取得
/**
*
*
* @return
*/
public static List getAllApps(Context context) {
PackageManager pm = context.getPackageManager();
List packages = pm.getInstalledPackages(0);
List list = new ArrayList();
for (PackageInfo info : packages) {
ApplicationInfo applicationInfo = info.applicationInfo;
String name = applicationInfo.loadLabel(pm).toString();
Drawable icon = applicationInfo.loadIcon(pm);
String sourceDir = applicationInfo.sourceDir;
File file = new File(sourceDir);
int flags = applicationInfo.flags;
boolean isInstallSD = false;
if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE) {
isInstallSD = true;
}
boolean isSystem = false;
if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
isSystem = true;
}
AppBean bean = new AppBean();
bean.icon = icon;
bean.name = name;
bean.size = file.length();
bean.isInstallSD = isInstallSD;
bean.isSystem = isSystem;
bean.packageName = info.packageName;
list.add(bean);
}
return list;
}
public static List getAllLaunchApps(Context context) {
PackageManager pm = context.getPackageManager();
List packages = pm.getInstalledPackages(0);
List list = new ArrayList();
for (PackageInfo info : packages) {
Intent intent = pm.getLaunchIntentForPackage(info.packageName);
if (intent == null) {
continue;
}
ApplicationInfo applicationInfo = info.applicationInfo;
String name = applicationInfo.loadLabel(pm).toString();
Drawable icon = applicationInfo.loadIcon(pm);
String sourceDir = applicationInfo.sourceDir;
File file = new File(sourceDir);
int flags = applicationInfo.flags;
boolean isInstallSD = false;
if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE) {
isInstallSD = true;
}
boolean isSystem = false;
if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
isSystem = true;
}
AppBean bean = new AppBean();
bean.icon = icon;
bean.name = name;
bean.size = file.length();
bean.isInstallSD = isInstallSD;
bean.isSystem = isSystem;
bean.packageName = info.packageName;
list.add(bean);
}
return list;
}