Android 获取当前打开的应用程序包名称
Android get package name of current opened application
我正在使用自定义键盘,当键盘打开时我需要当前的包名称。这个键盘将打开不同的不同应用程序,所以我需要当前打开的应用程序包名称。我正在使用以下代码,但它无法正常工作。
ActivityManager mActivityManager = (ActivityManager)PhotoActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT > 20){
String mPackageName = mActivityManager.getRunningAppProcesses().get(0).processName;
Log.e("Checking package: ","Checking current application package"+mPackageName);
}
else{
String mPackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
}
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("current task :", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getPackageName());
使用以下代码:-
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
} catch (Exception e) {
// Name Not FOund Exception
}
}
并且不要忘记使用权限
<uses-permission android:name="android.permission.GET_TASKS"/>
在android
中获取当前包名称
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
// display the current class name
Toast.makeText(getApplicationContext(), taskInfo.get(0).topActivity.getClassName(), Toast.LENGTH_LONG).show();
ComponentName componentInfo = taskInfo.get(0).topActivity;
// current class package name
String packageName = componentInfo.getPackageName();
将此添加到您的项目清单权限
<uses-permission android:name="android.permission.GET_TASKS"/>
我正在使用自定义键盘,当键盘打开时我需要当前的包名称。这个键盘将打开不同的不同应用程序,所以我需要当前打开的应用程序包名称。我正在使用以下代码,但它无法正常工作。
ActivityManager mActivityManager = (ActivityManager)PhotoActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
if(Build.VERSION.SDK_INT > 20){
String mPackageName = mActivityManager.getRunningAppProcesses().get(0).processName;
Log.e("Checking package: ","Checking current application package"+mPackageName);
}
else{
String mPackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
}
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("current task :", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getPackageName());
使用以下代码:-
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
} catch (Exception e) {
// Name Not FOund Exception
}
}
并且不要忘记使用权限
<uses-permission android:name="android.permission.GET_TASKS"/>
在android
中获取当前包名称 ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
// display the current class name
Toast.makeText(getApplicationContext(), taskInfo.get(0).topActivity.getClassName(), Toast.LENGTH_LONG).show();
ComponentName componentInfo = taskInfo.get(0).topActivity;
// current class package name
String packageName = componentInfo.getPackageName();
将此添加到您的项目清单权限
<uses-permission android:name="android.permission.GET_TASKS"/>