本文主要是介绍Android获取系统跑在前台应用包名(四十一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android获取系统跑在前台应用包名
import android.content.Context;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.app.ActivityManagerNative;
import android.app.IActivityController;
import android.app.IActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.util.Log;1.baseActivity和topActivity用法
frameworks/base/core/java/android/app/ActivityManager.java
public ComponentName baseActivity;
public ComponentName topActivity;2.获取当前系统最顶端Activity:
public void getPkname(){IActivityManager am = ActivityManagerNative.asInterface(ServiceManager.getService("activity"));try{ComponentName cn = am.getTasks(1, 0).get(0).topActivity;if("com.xxx.test".equals(cn.getPackageName())){Log.e(TAG, "xxx---------> pkname: " + cn.getPackageName());}else{Log.e(TAG, "xxx---------> pkname: " + cn.getPackageName());}}catch (RemoteException e) {e.rethrowFromSystemServer();}
}3.获取所有Activity:
public void getPkname() {IActivityManager am = ActivityManagerNative.asInterface(ServiceManager.getService("activity"));try{List<RunningTaskInfo> infos = am.getTasks(100,0);for(RunningTaskInfo info : infos) {Log.e(TAG, "xxx————> pkname: " + info.baseActivity.getPackageName());}}}catch (RemoteException e) {e.rethrowFromSystemServer();}}
这篇关于Android获取系统跑在前台应用包名(四十一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!