Example usage for android.content.pm PackageManager getLaunchIntentForPackage

List of usage examples for android.content.pm PackageManager getLaunchIntentForPackage

Introduction

In this page you can find the example usage for android.content.pm PackageManager getLaunchIntentForPackage.

Prototype

public abstract @Nullable Intent getLaunchIntentForPackage(@NonNull String packageName);

Source Link

Document

Returns a "good" intent to launch a front-door activity in a package.

Usage

From source file:net.wequick.small.Small.java

public static void preSetUp(Application context) {
    sContext = context;//from w ww  .  jav a  2 s.com

    // Register default bundle launchers
    registerLauncher(new ActivityLauncher());
    registerLauncher(new ApkBundleLauncher());
    registerLauncher(new WebBundleLauncher());

    PackageManager pm = context.getPackageManager();
    String packageName = context.getPackageName();

    // Check if host app is first-installed or upgraded
    int backupHostVersion = getHostVersionCode();
    int currHostVersion = 0;
    try {
        PackageInfo pi = pm.getPackageInfo(packageName, 0);
        currHostVersion = pi.versionCode;
    } catch (PackageManager.NameNotFoundException ignored) {
        // Never reach
    }

    if (backupHostVersion != currHostVersion) {
        sIsNewHostApp = true;
        setHostVersionCode(currHostVersion);
    } else {
        sIsNewHostApp = false;
    }

    // Collect host certificates
    try {
        Signature[] ss = pm.getPackageInfo(Small.getContext().getPackageName(),
                PackageManager.GET_SIGNATURES).signatures;
        if (ss != null) {
            int N = ss.length;
            sHostCertificates = new byte[N][];
            for (int i = 0; i < N; i++) {
                sHostCertificates[i] = ss[i].toByteArray();
            }
        }
    } catch (PackageManager.NameNotFoundException ignored) {

    }

    // Check if application is started after unexpected exit (killed in background etc.)
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ComponentName launchingComponent = am.getRunningTasks(1).get(0).topActivity;
    ComponentName launcherComponent = pm.getLaunchIntentForPackage(packageName).getComponent();
    if (!launchingComponent.equals(launcherComponent)) {
        // In this case, system launching the last restored activity instead of our launcher
        // activity. Call `setUp' synchronously to ensure `Small' available.
        setUp(context, null);
    }
}

From source file:jackpal.androidterm.TermPreferences.java

private void setAppPickerList() {
    PackageManager pm = this.getApplicationContext().getPackageManager();
    final List<ApplicationInfo> installedAppList = pm.getInstalledApplications(0);
    final TreeMap<String, String> items = new TreeMap<>();
    for (ApplicationInfo app : installedAppList) {
        Intent intent = pm.getLaunchIntentForPackage(app.packageName);
        if (intent != null)
            items.put(app.loadLabel(pm).toString(), app.packageName);
    }/*  www .  j av  a2  s  .com*/
    List<String> list = new ArrayList<>(items.keySet());
    final String labels[] = list.toArray(new String[list.size()]);
    list = new ArrayList<>(items.values());
    final String packageNames[] = list.toArray(new String[list.size()]);

    String id = "external_app_package_name";
    ListPreference packageName = (ListPreference) getPreferenceScreen().findPreference(id);
    packageName.setEntries(labels);
    packageName.setEntryValues(packageNames);
}

From source file:com.sentaroh.android.TaskAutomation.TaskExecutor.java

final static private void executeAndroidActivity(TaskManagerParms taskMgrParms, EnvironmentParms envParms,
        CommonUtilities util, TaskResponse task_response, ActionResponse ar, String task, String pkg,
        TaskActionItem eali) {/*from  w  ww.ja v a 2 s .  c  om*/
    util.addLogMsg("I", String.format(taskMgrParms.teMsgs.msgs_thread_task_exec_android, task, pkg));
    final PackageManager pm = taskMgrParms.context.getPackageManager();
    Intent in = pm.getLaunchIntentForPackage(pkg);
    ar.action_resp = ActionResponse.ACTION_SUCCESS;
    if (in != null) {
        in.setAction(Intent.ACTION_MAIN);
        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        if (eali.action_activity_data_type.equals(PROFILE_ACTION_TYPE_ACTIVITY_DATA_TYPE_URI)) {
            in.setData(Uri.parse(eali.action_activity_data_uri));
            if (envParms.settingDebugLevel >= 1)
                util.addDebugMsg(1, "I", "   Uri data added : Uri=", eali.action_activity_data_uri);
        } else if (eali.action_activity_data_type.equals(PROFILE_ACTION_TYPE_ACTIVITY_DATA_TYPE_EXTRA)) {
            ArrayList<ActivityExtraDataItem> aed_list = eali.action_activity_data_extra_list;
            for (int i = 0; i < aed_list.size(); i++) {
                ActivityExtraDataItem aedi = aed_list.get(i);
                if (aedi.data_value_array.equals(PROFILE_ACTION_TYPE_ACTIVITY_EXTRA_DATA_VALUE_ARRAY_NO)) {
                    String d_val_string = "";
                    boolean d_val_boolean = false;
                    int d_val_int = 0;
                    if (aedi.data_type.equals(PROFILE_ACTION_TYPE_ACTIVITY_EXTRA_DATA_VALUE_STRING)) {
                        d_val_string = aedi.data_value;
                        in.putExtra(aedi.key_value, d_val_string);
                        if (envParms.settingDebugLevel >= 1)
                            util.addDebugMsg(1, "I", "   Extra String data added : key=", aedi.key_value,
                                    ", value=", d_val_string);
                    } else if (aedi.data_type.equals(PROFILE_ACTION_TYPE_ACTIVITY_EXTRA_DATA_VALUE_INT)) {
                        d_val_int = Integer.valueOf(aedi.data_value);
                        in.putExtra(aedi.key_value, d_val_int);
                        if (envParms.settingDebugLevel >= 1)
                            util.addDebugMsg(1, "I", "   Extra Int data added : key=", aedi.key_value,
                                    ", value=", String.valueOf(d_val_int));
                    } else if (aedi.data_type.equals(PROFILE_ACTION_TYPE_ACTIVITY_EXTRA_DATA_VALUE_BOOLEAN)) {
                        if (aedi.data_value.equals("true"))
                            d_val_boolean = true;
                        in.putExtra(aedi.key_value, d_val_boolean);
                        if (envParms.settingDebugLevel >= 1)
                            util.addDebugMsg(1, "I", "   Extra Boolean data added : key=", aedi.key_value,
                                    ", value=", String.valueOf(d_val_boolean));
                    }
                } else if (aedi.data_value_array
                        .equals(PROFILE_ACTION_TYPE_ACTIVITY_EXTRA_DATA_VALUE_ARRAY_YES)) {
                    if (aedi.data_type.equals(PROFILE_ACTION_TYPE_ACTIVITY_EXTRA_DATA_VALUE_STRING)) {
                        String[] d_val_array = aedi.data_value.split("\u0003");
                        String[] d_val_extra = new String[d_val_array.length];
                        for (int ai = 0; ai < d_val_array.length; ai++) {
                            d_val_extra[ai] = d_val_array[ai];
                            if (envParms.settingDebugLevel >= 1)
                                util.addDebugMsg(1, "I", "   Extra array String data added : key=",
                                        aedi.key_value, ", value=", d_val_extra[ai]);
                        }
                        in.putExtra(aedi.key_value, d_val_extra);
                    } else if (aedi.data_type.equals(PROFILE_ACTION_TYPE_ACTIVITY_EXTRA_DATA_VALUE_INT)) {
                        String[] d_val_array = aedi.data_value.split("\u0003");
                        int[] d_val_extra = new int[d_val_array.length];
                        for (int ai = 0; ai < d_val_array.length; ai++) {
                            d_val_extra[ai] = Integer.valueOf(d_val_array[ai]);
                            if (envParms.settingDebugLevel >= 1)
                                util.addDebugMsg(1, "I", "   Extra array Int data added : key=", aedi.key_value,
                                        ", value=", String.valueOf(d_val_extra[ai]));
                        }
                        in.putExtra(aedi.key_value, d_val_extra);
                    } else if (aedi.data_type.equals(PROFILE_ACTION_TYPE_ACTIVITY_EXTRA_DATA_VALUE_BOOLEAN)) {
                        String[] d_val_array = aedi.data_value.split("\u0003");
                        boolean[] d_val_extra = new boolean[d_val_array.length];
                        for (int ai = 0; ai < d_val_array.length; ai++) {
                            if (d_val_array[ai].equals("true"))
                                d_val_extra[ai] = true;
                            else
                                d_val_extra[ai] = false;
                            if (envParms.settingDebugLevel >= 1)
                                util.addDebugMsg(1, "I", "   Extra array Boolean data added : key=",
                                        aedi.key_value, ", value=", String.valueOf(d_val_extra[ai]));
                        }
                        in.putExtra(aedi.key_value, d_val_extra);
                    }
                }
            }
        } else {
            if (envParms.settingDebugLevel >= 1)
                util.addDebugMsg(1, "I", "   No data was supplied");
        }
        taskMgrParms.context.startActivity(in);
        task_response.active_thread_ctrl.setThreadResultSuccess();
        waitTimeTc(task_response, 100);
    } else {
        String msg = String.format(taskMgrParms.teMsgs.msgs_thread_task_intent_notfound, pkg);
        util.addLogMsg("E", msg);
        ar.action_resp = ActionResponse.ACTION_ERROR;
        ar.resp_msg_text = msg;
    }
}

From source file:org.torproject.android.Orbot.java

private void startIntent(String pkg, String action, Uri data) {
    Intent i;/*  w w w  .  j  a  v  a 2s.  co m*/
    PackageManager manager = getPackageManager();
    try {
        i = manager.getLaunchIntentForPackage(pkg);
        if (i == null)
            throw new PackageManager.NameNotFoundException();
        i.setAction(action);
        i.setData(data);
        startActivity(i);
    } catch (PackageManager.NameNotFoundException e) {

    }
}

From source file:de.arcus.framework.activities.CrashActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_crash);

    // Reads the crash information
    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        if (bundle.containsKey(EXTRA_FLAG_CRASH_MESSAGE))
            mCrashMessage = bundle.getString(EXTRA_FLAG_CRASH_MESSAGE);
        if (bundle.containsKey(EXTRA_FLAG_CRASH_LOG))
            mCrashLog = bundle.getString(EXTRA_FLAG_CRASH_LOG);
    } else {/*from ww  w . j a va2s  .c om*/
        // No information; close activity
        finish();
        return;
    }

    try {
        // Get the PackageManager to load information about the app
        PackageManager packageManager = getPackageManager();
        // Loads the ApplicationInfo with meta data
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(),
                PackageManager.GET_META_DATA);

        if (applicationInfo.metaData != null) {
            // Reads the crash handler settings from meta data
            if (applicationInfo.metaData.containsKey("crashhandler.email"))
                mMetaDataEmail = applicationInfo.metaData.getString("crashhandler.email");
            if (applicationInfo.metaData.containsKey("crashhandler.supporturl"))
                mMetaDataSupportURL = applicationInfo.metaData.getString("crashhandler.supporturl");
        }

        // Gets the app name
        mAppName = packageManager.getApplicationLabel(applicationInfo).toString();
        // Gets the launch intent for the restart
        mLaunchIntent = packageManager.getLaunchIntentForPackage(getPackageName());

    } catch (PackageManager.NameNotFoundException ex) {
        // If this occurs then god must be already dead
        Logger.getInstance().logError("CrashHandler", ex.toString());
    }

    // Set the action bar title
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setTitle(getString(R.string.crashhandler_app_has_stopped_working, mAppName));
    }

    // Set the message
    TextView textViewMessage = (TextView) findViewById(R.id.text_view_crash_message);
    if (textViewMessage != null) {
        textViewMessage.setText(mCrashLog);
    }
}

From source file:com.google.samples.apps.sergio.ui.BaseActivity.java

private void launchIoHunt() {
    if (!TextUtils.isEmpty(Config.IO_HUNT_PACKAGE_NAME)) {
        LOGD(TAG, "Attempting to launch I/O hunt.");
        PackageManager pm = getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage(Config.IO_HUNT_PACKAGE_NAME);
        if (launchIntent != null) {
            // start I/O Hunt
            LOGD(TAG, "I/O hunt intent found, launching.");
            startActivity(launchIntent);
        } else {//  www  .j  av a  2 s  .c o  m
            // send user to the Play Store to download it
            LOGD(TAG, "I/O hunt intent NOT found, going to Play Store.");
            Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(Config.PLAY_STORE_URL_PREFIX + Config.IO_HUNT_PACKAGE_NAME));
            startActivity(intent);
        }
    }
}

From source file:com.saarang.samples.apps.iosched.ui.BaseActivity.java

private void launchIoHunt() {
    if (!TextUtils.isEmpty(Config.IO_HUNT_PACKAGE_NAME)) {
        LogUtils.LOGD(TAG, "Attempting to launch I/O hunt.");
        PackageManager pm = getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage(Config.IO_HUNT_PACKAGE_NAME);
        if (launchIntent != null) {
            // start I/O Hunt
            LogUtils.LOGD(TAG, "I/O hunt intent found, launching.");
            startActivity(launchIntent);
        } else {/*from   w ww. j a v a2s.c  o  m*/
            // send user to the Play Store to download it
            LogUtils.LOGD(TAG, "I/O hunt intent NOT found, going to Play Store.");
            Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(Config.PLAY_STORE_URL_PREFIX + Config.IO_HUNT_PACKAGE_NAME));
            startActivity(intent);
        }
    }
}

From source file:com.inmobi.ultrapush.AnalyzeActivity.java

private void getMaxValueFromFifo(double maxAmpFreq) {

    double currentFrequency = 0;
    int count = 0;
    for (Double d : fifoQueue) {
        if (Math.abs(d - maxAmpFreq) < 20)
            count++;/*from   w w  w.j a  v  a2 s  .  co  m*/
    }
    if (count > 5)
        currentFrequency = maxAmpFreq;

    if (count < 5)
        return;

    if (Math.abs((16000 - currentFrequency)) < 20) {
        Log.e("amlan", "<<<<<<16K>>>>>>");
        PackageManager pm = AnalyzeActivity.this.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage("com.flipkart.android");
        AnalyzeActivity.this.startActivity(launchIntent);
    } else if (Math.abs((16500 - currentFrequency)) < 20) {
        Log.e("amlan", "<<<<<<16.5K>>>>>>");
        PackageManager pm = AnalyzeActivity.this.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage("com.alibaba.aliexpresshd");
        AnalyzeActivity.this.startActivity(launchIntent);

    } else if (Math.abs((17000 - currentFrequency)) < 20) {
        Log.e("amlan", "<<<<<<17K>>>>>>");
        PackageManager pm = AnalyzeActivity.this.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage("com.snapdeal.main");
        AnalyzeActivity.this.startActivity(launchIntent);
    } else if (Math.abs((17500 - currentFrequency)) < 20) {
        Log.e("amlan", "<<<<<<17.5K>>>>>>");
        PackageManager pm = AnalyzeActivity.this.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage("com.myntra.android");
        AnalyzeActivity.this.startActivity(launchIntent);
    } else if (Math.abs((18000 - currentFrequency)) < 20) {
        Log.e("amlan", "<<<<<<18K>>>>>>");
        PackageManager pm = AnalyzeActivity.this.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage("net.one97.paytm");
        AnalyzeActivity.this.startActivity(launchIntent);
    } else if (Math.abs((18500 - currentFrequency)) < 20) {
        Log.e("amlan", "<<<<<<18.5K>>>>>>");
        PackageManager pm = AnalyzeActivity.this.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage("in.amazon.mShop.android.shopping");
        AnalyzeActivity.this.startActivity(launchIntent);
    } else if (Math.abs((19000 - currentFrequency)) < 20) {
        Log.e("amlan", "<<<<<<19K>>>>>>");
        PackageManager pm = AnalyzeActivity.this.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage("in.swiggy.android");
        AnalyzeActivity.this.startActivity(launchIntent);
    } else if (Math.abs((19500 - currentFrequency)) < 20) {
        Log.e("amlan", "<<<<<<19.5K>>>>>>");
        PackageManager pm = AnalyzeActivity.this.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage("com.ubercab");
        AnalyzeActivity.this.startActivity(launchIntent);
    } else if (Math.abs((20000 - currentFrequency)) < 20) {
        Log.e("amlan", "<<<<<<20K>>>>>>");
        PackageManager pm = AnalyzeActivity.this.getPackageManager();
        Intent launchIntent = pm.getLaunchIntentForPackage("com.global.foodpanda.android");
        AnalyzeActivity.this.startActivity(launchIntent);
    } else {
        Log.e("amlan", "<<<<<<Its not valid>>>>>>");
    }

}

From source file:org.restcomm.android.sdk.RCDevice.java

void onNotificationIntent(Intent intent) {
    String intentAction = intent.getAction();

    //if (!intentAction.equals(ACTION_NOTIFICATION_MESSAGE_DEFAULT)) {
    if (intentAction.equals(ACTION_NOTIFICATION_CALL_DEFAULT)
            || intentAction.equals(ACTION_NOTIFICATION_CALL_ACCEPT_VIDEO)
            || intentAction.equals(ACTION_NOTIFICATION_CALL_ACCEPT_AUDIO)
            || intentAction.equals(ACTION_NOTIFICATION_CALL_DECLINE)
            || intentAction.equals(ACTION_NOTIFICATION_CALL_DELETE)) {
        // The user has acted on a call notification, let's cancel it
        String username = intent.getStringExtra(EXTRA_DID).replaceAll(".*?sip:", "").replaceAll("@.*$", "");

        NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(callNotifications.get(username));

        //callNotifications.remove(username);

        activeCallNotification = false;/*from  w  w  w.j a v a 2 s . c om*/
    }

    Intent actionIntent = null;
    /*
    if (intentAction.equals(ACTION_NOTIFICATION_CALL_OPEN)) {
       RCConnection connection = getLiveConnection();
       if (connection != null) {
    if (connection.isIncoming()) {
       callIntent.setAction(ACTION_INCOMING_CALL);
    }
    else {
       callIntent.setAction(ACTION_OUTGOING_CALL);
    }
    callIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    // don't forget to copy the extras to callIntent
    callIntent.putExtras(intent);
    actionIntent = callIntent;
       }
    }
    */
    if (intentAction.equals(ACTION_NOTIFICATION_CALL_DEFAULT)) {
        if (callIntent != null) {
            callIntent.setAction(ACTION_INCOMING_CALL);
            // don't forget to copy the extras to callIntent
            callIntent.putExtras(intent);
            actionIntent = callIntent;
        } else {
            Context context = getApplicationContext();
            PackageManager packageManager = context.getPackageManager();
            actionIntent = packageManager.getLaunchIntentForPackage(context.getPackageName());
        }

    } else if (intentAction.equals(ACTION_NOTIFICATION_CALL_ACCEPT_VIDEO)) {
        callIntent.setAction(ACTION_INCOMING_CALL_ANSWER_VIDEO);
        // don't forget to copy the extras
        callIntent.putExtras(intent);
        actionIntent = callIntent;
    } else if (intentAction.equals(ACTION_NOTIFICATION_CALL_ACCEPT_AUDIO)) {
        callIntent.setAction(ACTION_INCOMING_CALL_ANSWER_AUDIO);
        // don't forget to copy the extras
        callIntent.putExtras(intent);

        actionIntent = callIntent;
    } else if (intentAction.equals(ACTION_NOTIFICATION_CALL_DECLINE)
            || intentAction.equals(ACTION_NOTIFICATION_CALL_DELETE)) {
        RCConnection pendingConnection = getPendingConnection();
        if (pendingConnection != null) {
            pendingConnection.reject();
        }

        release();

        // if the call has been requested to be declined, we shouldn't do any UI handling
        return;
    } else if (intentAction.equals(ACTION_NOTIFICATION_CALL_MUTE_AUDIO)) {
        RCConnection liveConnection = getLiveConnection();
        if (liveConnection != null) {
            if (liveConnection.isAudioMuted()) {
                liveConnection.setAudioMuted(false);
            } else {
                liveConnection.setAudioMuted(true);
            }
        }

        // if the call has been requested to be muted, we shouldn't do any UI handling
        return;
    } else if (intentAction.equals(ACTION_NOTIFICATION_CALL_DISCONNECT)) {
        RCConnection liveConnection = getLiveConnection();
        if (liveConnection != null) {
            liveConnection.disconnect();

            if (!isAttached()) {
                // if the call has been requested to be disconnected, we shouldn't do any UI handling
                callIntent.setAction(ACTION_CALL_DISCONNECT);
                //callIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                actionIntent = callIntent;
                startActivity(actionIntent);
            }

            /*
            // Important if we just trigger the call intent, then after the call is disconnected we will land to the previous screen in
            // that Task Stack, which is not what we want. Instead, we want the call activity to be finished and to just remain where
            // we were. To do that we need to create a new Task Stack were we only place the call activity
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            // Adds the target Intent to the top of the stack
            stackBuilder.addNextIntent(actionIntent);
            // Gets a PendingIntent containing the entire back stack, but with Component as the active Activity
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
            try {
               resultPendingIntent.send();
            }
            catch (PendingIntent.CanceledException e) {
               throw new RuntimeException("Pending Intent cancelled", e);
            }
            */
        }

        return;
    } else if (intentAction.equals(ACTION_NOTIFICATION_MESSAGE_DEFAULT)) {
        if (messageIntent == null) {
            storageManagerPreferences = new StorageManagerPreferences(this);
            String messageIntentString = storageManagerPreferences
                    .getString(RCDevice.ParameterKeys.INTENT_INCOMING_MESSAGE, null);
            if (messageIntentString != null) {
                try {
                    messageIntent = Intent.parseUri(messageIntentString, Intent.URI_INTENT_SCHEME);

                    //service was stopped and user taps on Notification case
                    if (!isInitialized()) {
                        HashMap<String, Object> parameters = StorageUtils.getParams(storageManagerPreferences);
                        try {
                            initialize(null, parameters, null);
                        } catch (RCException e) {
                            RCLogger.e(TAG, e.toString());
                        }
                    }
                } catch (URISyntaxException e) {
                    throw new RuntimeException("Failed to handle Notification");
                }
            }
        }
        messageIntent.setAction(ACTION_INCOMING_MESSAGE);

        // don't forget to copy the extras
        messageIntent.putExtras(intent);
        actionIntent = messageIntent;
        //we want to stop foreground (notification is tapped)
        stopForeground(true);
        stopRepeatingTask();
    } else {
        throw new RuntimeException("Failed to handle Notification");
    }

    // We need to create a Task Stack to make sure we maintain proper flow at all times
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds either call or message intent's Component in the stack together with all its parent activities (remember that for this to work the App Manifest needs to describe their relationship)
    stackBuilder.addParentStack(actionIntent.getComponent());
    // Adds the target Intent to the top of the stack
    stackBuilder.addNextIntent(actionIntent);
    // Gets a PendingIntent containing the entire back stack, but with Component as the active Activity
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    try {
        resultPendingIntent.send();
    } catch (PendingIntent.CanceledException e) {
        throw new RuntimeException("Pending Intent cancelled", e);
    }
}

From source file:biz.bokhorst.xprivacy.ActivityApp.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    // Check if running
    boolean running = false;
    ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    for (RunningAppProcessInfo info : activityManager.getRunningAppProcesses())
        if (info.uid == mAppInfo.getUid())
            running = true;/*from   w  w  w .java 2 s  .c  o  m*/

    PackageManager pm = getPackageManager();
    List<String> listPackageNames = mAppInfo.getPackageName();
    List<String> listApplicationName = mAppInfo.getApplicationName();
    for (int i = 0; i < listPackageNames.size(); i++) {
        Menu appMenu = (listPackageNames.size() == 1) ? menu
                : menu.addSubMenu(i, Menu.NONE, Menu.NONE, listApplicationName.get(i));

        // Launch
        MenuItem launch = appMenu.add(i, MENU_LAUNCH, Menu.NONE, getString(R.string.menu_app_launch));
        if (pm.getLaunchIntentForPackage(listPackageNames.get(i)) == null)
            launch.setEnabled(false);

        // Settings
        appMenu.add(i, MENU_SETTINGS, Menu.NONE, getString(R.string.menu_app_settings));

        // Kill
        MenuItem kill = appMenu.add(i, MENU_KILL, Menu.NONE, getString(R.string.menu_app_kill));
        kill.setEnabled(running && PrivacyManager.isApplication(mAppInfo.getUid()));

        // Play store
        MenuItem store = appMenu.add(i, MENU_STORE, Menu.NONE, getString(R.string.menu_app_store));
        if (!Util.hasMarketLink(this, listPackageNames.get(i)))
            store.setEnabled(false);
    }
}