下面列出了怎么用android.app.ActivityManager.RunningTaskInfo的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Constructs a {@link RunningTaskInfo} from a given {@param task}.
*/
private RunningTaskInfo createRunningTaskInfo(TaskRecord task) {
task.getNumRunningActivities(mTmpReport);
final RunningTaskInfo ci = new RunningTaskInfo();
ci.id = task.taskId;
ci.stackId = task.getStackId();
ci.baseActivity = mTmpReport.base.intent.getComponent();
ci.topActivity = mTmpReport.top.intent.getComponent();
ci.lastActiveTime = task.lastActiveTime;
ci.description = task.lastDescription;
ci.numActivities = mTmpReport.numActivities;
ci.numRunning = mTmpReport.numRunning;
ci.supportsSplitScreenMultiWindow = task.supportsSplitScreenWindowingMode();
ci.resizeMode = task.mResizeMode;
ci.configuration.setTo(task.getConfiguration());
return ci;
}
@SuppressLint({"NewApi"})
public static boolean isAppOnForeground(Context mContext) {
ActivityManager activityManager = (ActivityManager) mContext.getSystemService("activity");
String packageName = mContext.getPackageName();
LogInfo.log("PushReceiver", "packageName =" + packageName);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
LogInfo.log("PushReceiver", "------appProcesses == null-----");
return false;
}
for (RunningAppProcessInfo appProcess : appProcesses) {
LogInfo.log("PushReceiver", "------appProcess.processName =" + appProcess.processName);
if (appProcess.processName.equals(packageName) && appProcess.importance == 100) {
for (RunningTaskInfo rti : activityManager.getRunningTasks(100)) {
if (!(rti == null || rti.baseActivity == null || mContext.getPackageName() == null || !mContext.getPackageName().equals(rti.baseActivity.getPackageName()) || VERSION.SDK_INT < 11)) {
activityManager.moveTaskToFront(rti.id, 1);
}
}
return true;
}
}
return false;
}
public final void b() {
try {
ActivityManager activityManager = (ActivityManager) getSystemService(z[19]);
ComponentName componentName = ((RunningTaskInfo) activityManager.getRunningTasks(1).get(0)).baseActivity;
ComponentName componentName2 = ((RunningTaskInfo) activityManager.getRunningTasks(1).get(0)).topActivity;
new StringBuilder(z[17]).append(componentName.toString());
z.b();
new StringBuilder(z[18]).append(componentName2.toString());
z.b();
if (!(componentName == null || componentName2 == null || !componentName2.toString().equals(componentName.toString()))) {
c();
}
} catch (Exception e) {
c();
}
finish();
}
/**
* whether the app whost package's name is packageName is on the top of the stack
* <ul>
* <strong>Attentions:</strong>
* <li>You should add <strong>android.permission.GET_TASKS</strong> in manifest</li>
* </ul>
*
* @param context
* @param packageName
* @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of
* stack
*/
public static Boolean isTopActivity(Context context, String packageName) {
if (context == null || StringUtils.isEmpty(packageName)) {
return null;
}
ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (ListUtils.isEmpty(tasksInfo)) {
return null;
}
try {
return packageName.equals(tasksInfo.get(0).topActivity.getPackageName());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 获取当前堆栈中的第一个activity
* @param context Context
* @return 本APP当前运行的栈底组件(Activity)
* https://blog.csdn.net/u011386173/article/details/79095757 Andorid4.0系列可以,5.0以上机器不行 Android5.0此方法被废弃
* 对第三方APP无效,但对于获取本应用内的信息是有效的
*/
public static ComponentName getTheProcessBaseActivity(final Context context) {
ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager == null) {
return null;
}
List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);//这里有可能返回的为list size为0
if (runningTaskInfos == null || runningTaskInfos.isEmpty()) {
return null;
}
RunningTaskInfo task = runningTaskInfos.get(0);
if (task.numActivities > 0) {
CommonLog.d(TAG, "runningActivity topActivity=" + task.topActivity.getClassName());
CommonLog.d(TAG, "runningActivity baseActivity=" + task.baseActivity.getClassName());
return task.baseActivity;
}
return null;
}
/**
* 只能获取APP范围内的
* @param context
* @return
*/
public static ComponentName[] getAppTopAndBaseActivitys(Context context) {
ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager != null) {
List<RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
if (runningTaskInfos != null && runningTaskInfos.size() > 0) {
RunningTaskInfo topRunningTask = runningTaskInfos.get(0);
if (topRunningTask != null) {
if (topRunningTask.numActivities > 0) {
CommonLog.e(TAG, "---> getAppTopAndBaseActivitys() top:" + topRunningTask.topActivity/*.getPackageName()*/
+ " base:" + topRunningTask.baseActivity
);
ComponentName[] topAndBaseComponents = {
topRunningTask.topActivity,
topRunningTask.baseActivity
};
return topAndBaseComponents;
}
}
}
}
return null;
}
@SuppressLint("MissingPermission")
public static void moveAppTaskToFront(Context context) {
/**获取ActivityManager*/
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (activityManager == null) {
return;
}
/**获得当前运行的task(任务)*/
List<ActivityManager.RunningTaskInfo> taskInfoList = activityManager.getRunningTasks(100);
for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
/**找到本应用的 task,并将它切换到前台*/
if (taskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
activityManager.moveTaskToFront(taskInfo.id, 0);
break;
}
}
}
/**
* whether the app whost package's name is packageName is on the top of the
* stack
* <ul>
* <strong>Attentions:</strong>
* <li>You should add <strong>android.permission.GET_TASKS</strong> in
* manifest</li>
* </ul>
*
* @param context
* @param packageName
* @return if params error or task stack is null, return null, otherwise
* retun whether the app is on the top of stack
*/
public static Boolean isTopActivity(Context context, String packageName) {
if (context == null || TextUtils.isEmpty(packageName)) {
return null;
}
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (tasksInfo == null || tasksInfo.size() < 1) {
return null;
}
try {
return packageName.equals(tasksInfo.get(0).topActivity
.getPackageName());
} catch (Exception e) {
Logger.e(e);
return false;
}
}
/**
* 检测应用是否在前台
*
* Attentions:
* <ul>
* <li>You should add android.permission.GET_TASKS in manifest</li>
* </ul>
*
* @param context
* @param packageName
* @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of
* stack
*/
public static Boolean isTopActivity(Context context, String packageName) {
if (context == null || DataUtil.isEmpty(packageName)) {
return null;
}
ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (DataUtil.isEmpty(tasksInfo)) {
return null;
}
try {
return packageName.equals(tasksInfo.get(0).topActivity.getPackageName());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* whether the app package's name is on the top of the stack
* <ul>
* <strong>Attentions:</strong>
* <li>You should add <strong>android.permission.GET_TASKS</strong> in manifest</li>
* </ul>
*
* @param context
* @param packageName
* @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of
* stack
*/
public static boolean isTopActivity(Context context, String packageName) {
if (context == null || ObjectHelper.isEmpty(packageName)) {
return false;
}
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (ObjectHelper.isEmpty(tasksInfo)) {
return false;
}
try {
return packageName.equals(tasksInfo.get(0).topActivity.getPackageName());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean checkRootMethod11() {
Log.d(Main.TAG, "check12");
boolean returnValue = false;
// Get currently running application processes
List<RunningTaskInfo> list = manager.getRunningTasks(300);
if(list != null){
String tempName;
for(int i=0;i<list.size();++i){
tempName = list.get(i).baseActivity.flattenToString();
Log.d(Main.TAG, "baseActivity: " + tempName);
if(tempName.contains("supersu") || tempName.contains("superuser")){
Log.d(Main.TAG, "found one!");
returnValue = true;
}
}
}
return returnValue;
}
/**
* whether the app whost package's name is packageName is on the top of the stack
* <ul>
* Attentions:
* <li>You should add android.permission.GET_TASKS in manifest</li>
* </ul>
*
* @param context
* @param packageName
* @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of
* stack
*/
public static Boolean isTopActivity(Context context, String packageName) {
if (context == null || !BasicUtils.judgeNotNull(packageName)) {
return null;
}
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (!BasicUtils.judgeNotNull(tasksInfo)) {
return null;
}
try {
return packageName.equals(tasksInfo.get(0).topActivity.getPackageName());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* whether the app whost package's name is packageName is on the top of the stack
* <ul>
* <strong>Attentions:</strong>
* <li>You should add <strong>android.permission.GET_TASKS</strong> in manifest</li>
* </ul>
*
* @param context
* @param packageName
* @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of
* stack
*/
public static Boolean isTopActivity(Context context, String packageName) {
if (context == null || BasicUtils.judgeNotNull(packageName)) {
return null;
}
ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (BasicUtils.judgeNotNull(tasksInfo)) {
return null;
}
try {
return packageName.equals(tasksInfo.get(0).topActivity.getPackageName());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* whether the app whost package's name is packageName is on the top of the stack
* <ul>
* <strong>Attentions:</strong>
* <li>You should add <strong>android.permission.GET_TASKS</strong> in manifest</li>
* </ul>
*
* @param context
* @param packageName
* @return if params error or task stack is null, return null, otherwise retun whether the app is on the top of
* stack
*/
public static Boolean isTopActivity(Context context, String packageName) {
if (context == null || !BasicUtils.judgeNotNull(packageName)) {
return null;
}
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(1);
if (!BasicUtils.judgeNotNull(tasksInfo)) {
return null;
}
try {
return packageName.equals(tasksInfo.get(0).topActivity.getPackageName());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static ScreenState getScreenState(Context c){
PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
if(isScreenOn){
ActivityManager manager = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(Integer.MAX_VALUE);
RunningTaskInfo info = runningTaskInfos.get(0);
String nowPackageName = info.baseActivity.getPackageName();
if(nowPackageName.equals(MY_PACKAGE_NAME)){
return ScreenState.MYAPP;
}else{
return ScreenState.OTHERAPP;
}
// Log.e("MyUtils",nowPackageName);
}else{
// Log.e("isScreenOn:",String.valueOf(isScreenOn));
return ScreenState.LOCK;
}
}
public static boolean isTopActivy(String cmdName, Activity activity) {
List<RunningTaskInfo> runningTaskInfos = ((ActivityManager) activity.getSystemService("activity")).getRunningTasks(1);
String cmpNameTemp = null;
if (runningTaskInfos != null) {
cmpNameTemp = ((RunningTaskInfo) runningTaskInfos.get(0)).topActivity.getClassName();
}
if (cmpNameTemp == null) {
return false;
}
return cmpNameTemp.equals(cmdName);
}
void getTasks(int maxNum, List<RunningTaskInfo> list, @ActivityType int ignoreActivityType,
@WindowingMode int ignoreWindowingMode, SparseArray<ActivityDisplay> activityDisplays,
int callingUid, boolean allowed) {
// Return early if there are no tasks to fetch
if (maxNum <= 0) {
return;
}
// Gather all of the tasks across all of the tasks, and add them to the sorted set
mTmpSortedSet.clear();
mTmpStackTasks.clear();
final int numDisplays = activityDisplays.size();
for (int displayNdx = 0; displayNdx < numDisplays; ++displayNdx) {
final ActivityDisplay display = activityDisplays.valueAt(displayNdx);
for (int stackNdx = display.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
final ActivityStack stack = display.getChildAt(stackNdx);
stack.getRunningTasks(mTmpStackTasks, ignoreActivityType, ignoreWindowingMode,
callingUid, allowed);
for (int i = mTmpStackTasks.size() - 1; i >= 0; i--) {
mTmpSortedSet.addAll(mTmpStackTasks);
}
}
}
// Take the first {@param maxNum} tasks and create running task infos for them
final Iterator<TaskRecord> iter = mTmpSortedSet.iterator();
while (iter.hasNext()) {
if (maxNum == 0) {
break;
}
final TaskRecord task = iter.next();
list.add(createRunningTaskInfo(task));
maxNum--;
}
}
private static String getOldTopActivityPkgName(ActivityManager activityManager) {
try {
ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo) activityManager.getRunningTasks(1).get(0);
String packageName = info.topActivity.getPackageName();
if (packageName == null) {
return "";
}
return packageName;
} catch (Exception e) {
}
return "";
}
/**
* https://stackoverflow.com/questions/5446565/android-how-do-i-check-if-activity-is-running
*
* @param context Context
* @return boolean
*/
public static boolean isRunning(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo task : tasks) {
if (context.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}
public void run() {
List runningTasks = mWatchingService.mActivityManager.getRunningTasks(1);
String serviceName = ((RunningTaskInfo) runningTasks.get(0)).topActivity.getPackageName() + "\n"
+ ((RunningTaskInfo) runningTasks.get(0)).topActivity.getClassName();
if (!serviceName.equals(mWatchingService.serviceName)) {
mWatchingService.serviceName = serviceName;
if (DefaultSharedPreferences.read(mWatchingService)) {
mWatchingService.mHanlder.post(new RunView(this));
}
}
}
public void run() {
List runningTasks = mWatchingService.mActivityManager.getRunningTasks(1);
String serviceName = ((RunningTaskInfo) runningTasks.get(0)).topActivity.getPackageName() + "\n"
+ ((RunningTaskInfo) runningTasks.get(0)).topActivity.getClassName();
if (!serviceName.equals(mWatchingService.serviceName)) {
mWatchingService.serviceName = serviceName;
if (DefaultSharedPreferences.read(mWatchingService)) {
mWatchingService.mHanlder.post(new RunView(this));
}
}
}
public void onReceive(Context context, Intent intent)
{
switch (intent.getIntExtra("command", -1))
{
case 0:
showNotification(context, true);
ViewWindow.removeView();
DefaultSharedPreferences.save(context, false);
return;
case 1:
showNotification(context, false);
DefaultSharedPreferences.save(context, true);
if (VERSION.SDK_INT >= 21)
{
ViewWindow.showView(context, null);
return;
}
List runningTasks = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1);
ViewWindow.showView(context, ((RunningTaskInfo) runningTasks.get(0)).topActivity.getPackageName() + "\n"
+ ((RunningTaskInfo) runningTasks.get(0)).topActivity.getClassName());
return;
case 2:
ViewWindow.removeView();
DefaultSharedPreferences.save(context, false);
initNotification(context);
return;
default:
return;
}
}
/**
* @param @param context
* @param @return 设定文件
* @return String 返回类名
* @Title: getTopActivity
* @Description: 获取栈顶activity
*/
@SuppressWarnings("deprecation")
public static String getTopActivity(Context context) {
ActivityManager manager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
if (runningTaskInfos != null)
return (runningTaskInfos.get(0).topActivity.getClassName());
else
return "";
}
public static boolean isBackground(Context context) {
List<RunningTaskInfo> tasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1);
if (tasks.isEmpty() || ((RunningTaskInfo) tasks.get(0)).topActivity.getPackageName().equals(context.getPackageName())) {
return false;
}
return true;
}
public boolean isHome() {
ActivityManager mActivityManager = (ActivityManager) getSystemService("activity");
List<String> homeList = getHomes();
List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
if (homeList != null) {
return homeList.contains(((RunningTaskInfo) rti.get(0)).topActivity.getPackageName());
}
return false;
}
@SuppressLint({"NewApi"})
private void pushLiveOver() {
LetvConstant.setForcePlay(false);
StatisticsUtils.setActionProperty(NetworkUtils.DELIMITER_LINE, -1, NetworkUtils.DELIMITER_LINE);
if (MainActivity.getInstance() != null) {
if (this.isShowToast) {
UIsUtils.showToast(TipUtils.getTipMessage("70004", 2131100703));
}
ActivityManager am = (ActivityManager) this.mContext.getSystemService("activity");
for (RunningTaskInfo rti : am.getRunningTasks(100)) {
if (!(rti == null || rti.baseActivity == null || this.mContext.getPackageName() == null || !this.mContext.getPackageName().equals(rti.baseActivity.getPackageName()))) {
if (VERSION.SDK_INT >= 11) {
am.moveTaskToFront(rti.id, 1);
}
LogInfo.log("push_", "moveTaskToFront rti.id = " + rti.id);
}
}
if (TextUtils.isEmpty(this.cid)) {
MainLaunchUtils.launch(MainActivity.getInstance(), true);
} else if (!(this.cid.equals("3") || this.cid.equals("8") || this.cid.equals("4") || this.cid.equals("9"))) {
MainLaunchUtils.launch(MainActivity.getInstance(), true);
}
LogInfo.log("push_", "pushLiveOver live pushCid " + this.cid);
return;
}
if (TextUtils.isEmpty(this.cid)) {
MainLaunchUtils.launch(this.mContext, true);
} else if (this.cid.equals("3")) {
MainLaunchUtils.launchGotoLive(this.mContext, "ent", null, this.isShowToast, true);
} else if (this.cid.equals("8")) {
MainLaunchUtils.launchGotoLive(this.mContext, "other", null, this.isShowToast, true);
} else if (this.cid.equals("4")) {
MainLaunchUtils.launchGotoLive(this.mContext, "sports", null, this.isShowToast, true);
} else if (this.cid.equals("9")) {
MainLaunchUtils.launchGotoLive(this.mContext, "music", null, this.isShowToast, true);
} else {
MainLaunchUtils.launch(this.mContext, true);
}
LogInfo.log("push_", "pushLiveOver MainActivity.getInstance() == null no jump to live");
}
public static boolean isRunningForeground(Context context) {
String currentPackageName = ((RunningTaskInfo) ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1).get(0)).topActivity.getPackageName();
String appPackageName = context.getPackageName();
if (TextUtils.isEmpty(currentPackageName) || !currentPackageName.equals(appPackageName)) {
return false;
}
return true;
}
public static boolean isApplicationInBackground(Context context) {
List<RunningTaskInfo> tasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1);
if (tasks.isEmpty() || ((RunningTaskInfo) tasks.get(0)).topActivity.getPackageName().equals(context.getPackageName())) {
return false;
}
return true;
}
public static boolean isTopRunning(Context context) {
List<RunningTaskInfo> tasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = ((RunningTaskInfo) tasks.get(0)).topActivity;
LogInfo.log("plugin", "topActivity = " + topActivity.toString());
LogInfo.log("plugin", "topActivity.getPackageName() = " + topActivity.getPackageName());
LogInfo.log("plugin", "context.getPackageName() = " + context.getPackageName());
if (topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
private static boolean k(Context context) {
try {
if (d.b(context, "android.permission.GET_TASKS")) {
List runningTasks = ((ActivityManager) context.getSystemService("activity")).getRunningTasks(1);
if (!(runningTasks == null || runningTasks.isEmpty())) {
return ((RunningTaskInfo) runningTasks.get(0)).topActivity.getPackageName().equals(context.getPackageName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}