Is process name same as package name in android?

2019-04-06 10:15发布

By process I mean what we provide in android:process and by package I mean package in

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.osg.appkiller"
    android:versionCode="1"
    android:versionName="1.0" >

More details Processes and Threads - Android Developer

I wanted to get application names of all running apps. So this is what I did after looking at various sources (and it works).

    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    PackageManager packageManager = getPackageManager();
    final List<RunningAppProcessInfo> runningProcesses = activityManager.getRunningAppProcesses();
    for(RunningAppProcessInfo processInfo : runningProcesses) {
        CharSequence appName = null;
        try {
            appName = packageManager.getApplicationLabel(packageManager.getApplicationInfo(processInfo.processName, PackageManager.GET_META_DATA));
        } catch (NameNotFoundException e) {
            Log.e(TAG,"Application info not found for process : " + processInfo.processName,e);
        }
    }

If you see Documentation for PackageManager.getApplicationInfo

ApplicationInfo android.content.pm.PackageManager.getApplicationInfo(String packageName, int flags) throws NameNotFoundException

but I am passing

processInfo.processName

where processName is name of process running. So we are basically using process name as package name to get application info.

  1. First of all is this approach correct ?
  2. Secondly is it true that if we do not provide process for activities/services etc new process is created with same name as that of package name ?

1条回答
戒情不戒烟
2楼-- · 2019-04-06 11:20

By default android takes the package name as the process name. But if you define process property in application tag in manifest file android:process="com.example.newprocessname" then the application will run with this name "com.example.newprocessname".

As for your questions,

Hope this answers your question!

查看更多
登录 后发表回答