All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mobi.cangol.mobile.utils.AppUtils Maven / Gradle / Ivy

There is a newer version: 1.2.7
Show newest version
/**
 * Copyright (c) 2013 Cangol
 * 

* Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package mobi.cangol.mobile.utils; import android.app.Application; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.graphics.drawable.Drawable; import android.net.Uri; import android.util.DisplayMetrics; import java.util.ArrayList; import java.util.List; import java.util.Locale; public class AppUtils { private AppUtils() { } /** * 修改locale ,修改后只对新生成activity有效,旧的activity需要重启 * * @param context * @param locale */ public static void changeLocale(Context context, Locale locale) { final Configuration config = context.getResources().getConfiguration(); final DisplayMetrics dm = context.getResources().getDisplayMetrics(); config.locale = locale; context.getResources().updateConfiguration(config, dm); final Application app = (Application) context.getApplicationContext(); app.onConfigurationChanged(config); } /** * 安装apk文件 * * @param context * @param apkUri */ public static void install(Context context, Uri apkUri) { final Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); context.startActivity(intent); } /** * 安装apk文件 * * @param context * @param apkPath */ public static void install(Context context, String apkPath) { final Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(Uri.parse("file://" + apkPath), "application/vnd.android.package-archive"); context.startActivity(intent); } /** * 卸载应用 * * @param context * @param packageName */ public static void unInstall(Context context, String packageName) { final Uri uri = Uri.parse("package:" + packageName); final Intent intent = new Intent(Intent.ACTION_DELETE, uri); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } /** * 启动应用 * * @param context * @param packageName */ public static void launch(Context context, String packageName) { final PackageManager packageManager = context.getPackageManager(); final Intent intent = packageManager.getLaunchIntentForPackage(packageName); context.startActivity(intent); } /** * 判断是否安装应用 * * @param context * @param packageName * @return */ public static boolean isInstalled(Context context, String packageName) { final PackageManager pManager = context.getPackageManager(); final List apps = pManager.getInstalledPackages(0); for (int i = 0; i < apps.size(); i++) { if (apps.get(i).packageName.equals(packageName)) { return true; } } return false; } /** * 获取apk应用包名 * * @param context * @param apkPath * @return */ public static String getPackageName(Context context, String apkPath) { final ApplicationInfo appInfo = getApplicationInfo(context, apkPath); if (appInfo != null) { return appInfo.packageName; } return null; } /** * 获取apk应用icon * * @param context * @param apkPath * @return */ public static Drawable getApplicationIcon(Context context, String apkPath) { final PackageManager packageManager = context.getPackageManager(); final ApplicationInfo appInfo = getApplicationInfo(context, apkPath); if (appInfo != null) { appInfo.sourceDir = apkPath; appInfo.publicSourceDir = apkPath; return packageManager.getApplicationIcon(appInfo); } return null; } /** * 获取应用信息 * * @param context * @param apkPath * @return */ public static ApplicationInfo getApplicationInfo(Context context, String apkPath) { final PackageManager packageManager = context.getPackageManager(); final PackageInfo info = packageManager.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES); if (info != null) { return info.applicationInfo; } return null; } /** * 获取所有非系统应用 * * @param context * @return */ public static List getAllApps(Context context) { final List apps = new ArrayList<>(); final PackageManager pManager = context.getPackageManager(); final List pkgList = pManager.getInstalledPackages(0); for (int i = 0; i < pkgList.size(); i++) { final PackageInfo pak = pkgList.get(i); //not system app if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) { apps.add(pak); } } return apps; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy