记录一写Android常用API
Location.getLatitude() 纬度
Location.getLongitude() 经度
获取 运行 应用包名 顶层交互 TOP包名 主Activity
//获取当前系统中 正在运行的 APK包名
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE) ;
List<ActivityManager.RunningAppProcessInfo> appList = mActivityManager.getRunningAppProcesses() ;
for (RunningAppProcessInfo running : appList) {
if (running.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
Log.i("TAG","RunningAppName = "+running.processName);
}
}
//获取顶层交互 TOP包名
ComponentName cn = mActivityManager.getRunningTasks(1).get(0).topActivity;
String packageName = cn.getPackageName();
//根据包名 获取主Activity
PackageManager mPackageManager = context.getPackageManager();
Intent it = new Intent(Intent.ACTION_MAIN);
it.setPackage(packageName);// pkg为包名
it.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName ac = it.resolveActivity(mPackageManager);// mPackageManager为PackageManager实例
if(ac != null){ //这里判断下 因为ac对象可能为NULL
String classname = ac.getClassName();//此处得到Activity classname
}
说明:resolveActivity()方法默认使用MATCH_DEFAULT_ONLY的flag ,是指activity中的catetory必须要包含Android.intent.category.DEFAULT类型,否则会返回null。
最后不要忘记在AndroidManifest.xml中增加权限:
<uses-permission android:name = “android.permission.GET_TASKS”/>
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
RunningTaskInfo info = manager.getRunningTasks(1).get(0);
String shortClassName = info.topActivity.getShortClassName(); //类名
String className = info.topActivity.getClassName(); //完整类名
String packageName = info.topActivity.getPackageName(); //包名