首页 > 代码库 > 获取当前Activity名称

获取当前Activity名称

//获取当前的Activity名称 package.classname

private String getTopActivity(Activity context){

 ActivityManager am= (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

       

      List<RunningTaskInfo> runningTasks = am.getRunningTasks(1);

      

      RunningTaskInfo rti = runningTasks.get(0);

      ComponentName cn = rti.topActivity;

      

      Log.v("offline",  "getClassName = " + cn.getClassName());//包名+类名

      Log.v("offline",  "getShortClassName = " + cn.getShortClassName());//类名

      Log.v("offline",  "getPackageName = " + cn.getPackageName());//包名

      

      return cn.getClassName();

}


少不了权限:

  <!-- 获取activity名称 -->

        <uses-permission android:name = "android.permission.GET_TASKS"/>


本文出自 “爬过山见过海” 博客,请务必保留此出处http://670176656.blog.51cto.com/4500575/1427036