Is process name same as package name in android?

2019-04-06 10:53发布

问题:

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:

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,

  • 1-> In this case your application name is same as the default package name, that's why it is working. Try changing the process name. It'll not work.

  • 2-> That's true. It is by default. Refer to "android:process" in the following link: https://developer.android.com/guide/topics/manifest/application-element.html

Hope this answers your question!