Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

public class Main {
    public static String getAppName(Context context, Intent appIntent) {

        if (appIntent.hasExtra(Intent.EXTRA_SHORTCUT_NAME)) {
            return appIntent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
        }

        if (appIntent.hasExtra(Intent.EXTRA_SHORTCUT_INTENT)) {
            appIntent = appIntent.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT);
        }
        ComponentName componentName = appIntent.getComponent();

        PackageManager pm = context.getPackageManager();

        ApplicationInfo appInfo = null;
        ActivityInfo activityInfo = null;
        try {
            appInfo = pm.getApplicationInfo(componentName.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            appInfo = null;
        }
        try {
            activityInfo = pm.getActivityInfo(componentName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (appInfo == null) {
            return null;
        } else {
            CharSequence appName = pm.getApplicationLabel(appInfo);
            CharSequence activityName = null;

            if (activityInfo != null) {
                activityName = activityInfo.loadLabel(pm);
            }

            if (activityName != null) {
                return activityName.toString();
            }

            if (appName != null) {
                appName.toString();
            }

            return null;
        }
    }
}