android 如何获取当前 Activity 的类名和包名
其一:getClass().getSimpleName()
public static String getTopActivity(Context context){
ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
// Log.d("Chunna.zheng", "pkg:"+cn.getPackageName());//包名
// Log.d("Chunna.zheng", "cls:"+cn.getClassName());//包名加类名
return cn.getClass().getSimpleName();
}
其二:getClassName();
private String getCurrentActivityName() {
ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
return componentInfo.getClassName();
}
其三:getPacakgeName() +是否在当前Task的栈顶
private String getTopActivityPacakgeName(){
String currentApp = "CurrentNULL";
UsageStatsManager usm = (UsageStatsManager) mContext.getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
if (appList != null && appList.size() > 0) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
for (UsageStats usageStats : appList) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
Log.d("SideKey", "currentApp = "+currentApp);
return currentApp;
}
其四:getProcessNameByActivityName() 获取跳转的activity所在的进程Id 【不常用】
public static String getProcessNameByActivityName(Context context, String activityName) {
PackageInfo mPackageInfo = null;
try {
mPackageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (mPackageInfo != null) {
for (ActivityInfo activityInfo : mPackageInfo.activities) {
if (activityInfo.name != null && activityInfo.name.endsWith(activityName)) {
//activityInfo.processName是activity所在的进程
//activityInfo.packageName是项目的包名,也就是com.**.application,并非进程名称
return activityInfo.processName;
}
}
}
return "";}
其五:isRunning() 判断当前进程是否已启动 【不常用】
public static boolean isRunning(Context context, String packageName) {
ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> infos = am.getRunningAppProcesses();
if (infos == null) {
return false;
}
for (ActivityManager.RunningAppProcessInfo rapi : infos) {
if (rapi.processName.equals(packageName))
return true;
}
return false;
}
其六:isServiceRunning() : 获取当前运行的service 【不常用】
public static boolean isServiceRunning(String serviceClassName){
final ActivityManager activityManager = (ActivityManager)
Application.getContext().getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningServiceInfo> services =
activityManager.getRunningServices(Integer.MAX_VALUE);
for (RunningServiceInfo runningServiceInfo : services) {
if (runningServiceInfo.service.getClassName().equals(serviceClassName)){
return true;
}
}
return false;
}
更多方法获取了解:
分别得到当前类的类名和包名
String localClassName = getLocalClassName(); //String ClassName = getClassName();
String packageName = getPackageName();
String name = getClass().getName();String typename = getClass().getTypeName()
String simpleName = getClass().getSimpleName();
String canonicalName = getClass().getCanonicalName();举例:
1.localClassName = "MainActivity"
2.packageName = "com.android.server"
3.name = "com.android.server.MainActivity"4.typename = "com.android.server.MainActivity"
5.simpleName = "MainActivity"
6.canonicalName = "com.android.server.MainActivity"