概述
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.content.pm.ResolveInfo; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.util.ArrayMap; | |
import java.lang.reflect.Field; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* <pre> | |
* author: Blankj | |
* blog : http://blankj.com | |
* time : 2016/9/23 | |
* desc : Activity相关工具类 | |
* </pre> | |
*/ | |
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(StringpackageName,String className) { | |
Intent intent = new Intent(); | |
intent.setClassName(packageName, className); | |
return !(Utils.getContext().getPackageManager().resolveActivity(intent,0)== null || | |
intent.resolveActivity(Utils.getContext().getPackageManager())==null || | |
Utils.getContext().getPackageManager().queryIntentActivities(intent,0).size()==0); | |
} | |
/** | |
* 打开Activity | |
* | |
* @param packageName 包名 | |
* @param className 全类名 | |
*/ | |
public static void launchActivity(StringpackageName,String className) { | |
launchActivity(packageName, className, null); | |
} | |
/** | |
* 打开Activity | |
* | |
* @param packageName 包名 | |
* @param className 全类名 | |
* @param bundle bundle | |
*/ | |
public static void launchActivity(StringpackageName,String className, Bundle bundle) { | |
Utils.getContext().startActivity(IntentUtils.getComponentIntent(packageName, className, bundle)); | |
} | |
/** | |
* 获取launcher activity | |
* | |
* @param packageName 包名 | |
* @return launcher activity | |
*/ | |
public static String getLauncherActivity(StringpackageName) { | |
Intent intent = new Intent(Intent.ACTION_MAIN,null); | |
intent.addCategory(Intent.CATEGORY_LAUNCHER); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
PackageManager pm = Utils.getContext().getPackageManager(); | |
List<ResolveInfo> infos= pm.queryIntentActivities(intent,0); | |
for (ResolveInfo info: infos) { | |
if (info.activityInfo.packageName.equals(packageName)) { | |
return info.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; | |
} | |
} |
最后
以上就是大意抽屉为你收集整理的Activity相关→ActivityUtils的全部内容,希望文章能够帮你解决Activity相关→ActivityUtils所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复