- package com.anew.note.utils;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import java.io.File;
import java.util.List;
/**
* Created by starry on 2016/8/2.
*/
public class AppUtils {
private AppUtils() {
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 安装指定路径下的Apk
*/
public static void installApk(Activity activity, String filePath, int requsetCode) {
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
activity.startActivityForResult(intent, requsetCode);
}
/**
* 卸载指定包名的App
*/
public static void uninstallApp(Activity activity, String packageName, int requsetCode) {
Intent intent = new Intent();
intent.setAction("android.intent.action.DELETE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:" + packageName));
activity.startActivityForResult(intent, requsetCode);
}
/**
* 获取App名称
*/
public static String getAppName(Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(
context.getPackageName(), 0);
int labelRes = packageInfo.applicationInfo.labelRes;
return context.getResources().getString(labelRes);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取当前App版本号
*/
public static String getVersionName(Context context) {
String versionName = null;
PackageManager pm = context.getPackageManager();
PackageInfo info = null;
try {
info = pm.getPackageInfo(context.getApplicationContext().getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (info != null) {
versionName = info.versionName;
}
return versionName;
}
/**
* 获取当前App版本Code
*/
public static int getVersionCode(Context context) {
int versionCode = 0;
PackageManager pm = context.getPackageManager();
PackageInfo info = null;
try {
info = pm.getPackageInfo(context.getApplicationContext().getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (info != null) {
versionCode = info.versionCode;
}
return versionCode;
}
/**
* 打开指定包名的App
*/
public static void openOtherApp(Activity activity, String packageName) {
PackageManager manager = activity.getPackageManager();
Intent launchIntentForPackage = manager.getLaunchIntentForPackage(packageName);
if (launchIntentForPackage != null) {
activity.startActivity(launchIntentForPackage);
}
}
/**
* 打开指定包名的App应用信息界面
*/
public static void showAppInfo(Activity activity, String packageName) {
Intent intent = new Intent();
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.parse("package:" + packageName));
activity.startActivity(intent);
}
/**
* 分享Apk信息
*/
public static void shareApkInfo(Activity activity, String info) {
Intent intent = new Intent();
intent.setAction("android.intent.action.SEND");
intent.addCategory("android.intent.category.DEFAULT");
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, info);
activity.startActivity(intent);
}
/**
* 判断当前App处于前台还是后台
* 需添加<uses-permission android:name="android.permission.GET_TASKS"/>
* 并且必须是系统应用该方法才有效
*/
public static boolean isApplicationBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
@SuppressWarnings("deprecation")
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
}