安卓 启动activity工具类
### ActivityUtils.java```
public final class ActivityUtils {
private ActivityUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* 判断是否存在Activity
*
* @Param packageName 包名
* @param className activity全路径类名
* @Return {@Code true}: 是<br>{@code false}: 否
*/
public static boolean isActivityExists(Context context,String packageName, String className) {
Intent intent = new Intent();
intent.setClassName(packageName, className);
return !(context.getPackageManager().resolveActivity(intent, 0) == null ||
intent.resolveActivity(context.getPackageManager()) == null ||
context.getPackageManager().queryIntentActivities(intent, 0).size() == 0);
}
/**
* 启动Activity
*
* @param activity activity
* @param cls activity类
*/
public static void startActivity(Activity activity, Class<?> cls) {
startActivity(activity, null, activity.getPackageName(), cls.getName(), null);
}
/**
* 启动Activity
*
* @param extras extras
* @param activity activity
* @param cls activity类
*/
public static void startActivity(Bundle extras, Activity activity, Class<?> cls) {
startActivity(activity, extras, activity.getPackageName(), cls.getName(), null);
}
/**
* 启动Activity
*
* @param activityactivity
* @param cls activity类
* @param enterAnim 入场动画
* @param exitAnim出场动画
*/
public static void startActivity(Activity activity, Class<?> cls, int enterAnim, int exitAnim) {
startActivity(activity, null, activity.getPackageName(), cls.getName(), null);
activity.overridePendingTransition(enterAnim, exitAnim);
}
/**
* 启动Activity
*
* @param extras extras
* @param activityactivity
* @param cls activity类
* @param enterAnim 入场动画
* @param exitAnim出场动画
*/
public static void startActivity(Bundle extras, Activity activity, Class<?> cls, int enterAnim, int exitAnim) {
startActivity(activity, extras, activity.getPackageName(), cls.getName(), null);
activity.overridePendingTransition(enterAnim, exitAnim);
}
/**
* 启动Activity
*
* @param activity activity
* @param cls activity类
* @param options跳转动画
*/
public static void startActivity(Activity activity, Class<?> cls, Bundle options) {
startActivity(activity, null, activity.getPackageName(), cls.getName(), options);
}
/**
* 启动Activity
*
* @param extras extras
* @param activity activity
* @param cls activity类
* @param options跳转动画
*/
public static void startActivity(Bundle extras, Activity activity, Class<?> cls, Bundle options) {
startActivity(activity, extras, activity.getPackageName(), cls.getName(), options);
}
/**
* 启动Activity
*
* @param pkg 包名
* @param cls 全类名
*/
public static void startActivity(Context context,String pkg, String cls) {
startActivity(context, null, pkg, cls, null);
}
/**
* 启动Activity
*
* @param extras extras
* @param pkg 包名
* @param cls 全类名
*/
public static void startActivity(Context context,Bundle extras, String pkg, String cls) {
startActivity(context, extras, pkg, cls, extras);
}
/**
* 启动Activity
*
* @param pkg 包名
* @param cls 全类名
* @param options 动画
*/
public static void startActivity(Context context,String pkg, String cls, Bundle options) {
startActivity(context, null, pkg, cls, options);
}
/**
* 启动Activity
*
* @param extrasextras
* @param pkg 包名
* @param cls 全类名
* @param options 动画
*//*
public static void startActivity(Context context,Bundle extras, String pkg, String cls, Bundle options) {
startActivity(Utils.getContext(), extras, pkg, cls, options);
}*/
private static void startActivity(Context context, Bundle extras, String pkg, String cls, Bundle options) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (extras != null) intent.putExtras(extras);
intent.setComponent(new ComponentName(pkg, cls));
if (!(context instanceof Activity)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
if (options == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
context.startActivity(intent);
} else {
context.startActivity(intent, options);
}
}
/**
* 获取launcher activity
*
* @param packageName 包名
* @return launcher activity
*/
public static String getLauncherActivity(Context context,String packageName) {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PackageManager pm = context.getPackageManager();
List<ResolveInfo> info = pm.queryIntentActivities(intent, 0);
for (ResolveInfo aInfo : info) {
if (aInfo.activityInfo.packageName.equals(packageName)) {
return aInfo.activityInfo.name;
}
}
return "no " + packageName;
}
/**
* 获取栈顶Activity
*
* @return 栈顶Activity
*/
public static Activity getTopActivity() {
try {
Class activityThreadClass = Class.forName("android.app.ActivityThread");
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
activitiesField.setAccessible(true);
Map activities = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
activities = (HashMap) activitiesField.get(activityThread);
} else {
activities = (ArrayMap) activitiesField.get(activityThread);
}
for (Object activityRecord : activities.values()) {
Class activityRecordClass = activityRecord.getClass();
Field pausedField = activityRecordClass.getDeclaredField("paused");
pausedField.setAccessible(true);
if (!pausedField.getBoolean(activityRecord)) {
Field activityField = activityRecordClass.getDeclaredField("activity");
activityField.setAccessible(true);
return (Activity) activityField.get(activityRecord);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
``` 安卓上面好像是有一个现成的叫Quick shortcutMaker 看完也不知道是什麼用的 看看是什么情况。
页:
[1]