Example usage for android.content Intent setComponent

List of usage examples for android.content Intent setComponent

Introduction

In this page you can find the example usage for android.content Intent setComponent.

Prototype

public @NonNull Intent setComponent(@Nullable ComponentName component) 

Source Link

Document

(Usually optional) Explicitly set the component to handle the intent.

Usage

From source file:org.opendatakit.survey.android.activities.MainMenuActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == MENU_FILL_FORM) {
        swapToFragmentView(ScreenList.FORM_CHOOSER);
        return true;
    } else if (item.getItemId() == MENU_PULL_FORMS) {
        swapToFragmentView(ScreenList.FORM_DOWNLOADER);
        return true;
    } else if (item.getItemId() == MENU_CLOUD_FORMS) {
        try {//www  .j  a  va  2s  .  c  o  m
            Intent syncIntent = new Intent();
            syncIntent.setComponent(
                    new ComponentName("org.opendatakit.sync", "org.opendatakit.sync.activities.SyncActivity"));
            syncIntent.setAction(Intent.ACTION_DEFAULT);
            Bundle bundle = new Bundle();
            bundle.putString(APP_NAME, appName);
            syncIntent.putExtras(bundle);
            this.startActivityForResult(syncIntent, SYNC_ACTIVITY_CODE);
        } catch (ActivityNotFoundException e) {
            WebLogger.getLogger(getAppName()).printStackTrace(e);
            Toast.makeText(this, R.string.sync_not_found, Toast.LENGTH_LONG).show();
        }
        return true;
    } else if (item.getItemId() == MENU_MANAGE_FORMS) {
        swapToFragmentView(ScreenList.FORM_DELETER);
        return true;
    } else if (item.getItemId() == MENU_EDIT_INSTANCE) {
        swapToFragmentView(ScreenList.WEBKIT);
        return true;
    } else if (item.getItemId() == MENU_PUSH_FORMS) {
        swapToFragmentView(ScreenList.INSTANCE_UPLOADER_TABLE_CHOOSER);
        return true;
    } else if (item.getItemId() == MENU_PREFERENCES) {
        // PreferenceFragment missing from support library...
        Intent ig = new Intent(this, PreferencesActivity.class);
        // TODO: convert this activity into a preferences fragment
        ig.putExtra(APP_NAME, getAppName());
        startActivity(ig);
        return true;
    } else if (item.getItemId() == MENU_ADMIN_PREFERENCES) {
        String pw = PropertiesSingleton.getProperty(appName, AdminPreferencesActivity.KEY_ADMIN_PW);
        if (pw == null || "".equalsIgnoreCase(pw)) {
            Intent i = new Intent(getApplicationContext(), AdminPreferencesActivity.class);
            // TODO: convert this activity into a preferences fragment
            i.putExtra(APP_NAME, getAppName());
            startActivity(i);
        } else {
            createPasswordDialog();
        }
        return true;
    } else if (item.getItemId() == MENU_ABOUT) {
        swapToFragmentView(ScreenList.ABOUT_MENU);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

From source file:cm.aptoide.com.actionbarsherlock.widget.SearchView.java

/**
 * Create and return an Intent that can launch the voice search activity, perform a specific
 * voice transcription, and forward the results to the searchable activity.
 *
 * @param baseIntent The voice app search intent to start from
 * @return A completely-configured intent ready to send to the voice search activity
 *//*  w  ww  . j av a 2s. c  o  m*/
private Intent createVoiceAppSearchIntent(Intent baseIntent, SearchableInfo searchable) {
    ComponentName searchActivity = searchable.getSearchActivity();

    // create the necessary intent to set up a search-and-forward operation
    // in the voice search system.   We have to keep the bundle separate,
    // because it becomes immutable once it enters the PendingIntent
    Intent queryIntent = new Intent(Intent.ACTION_SEARCH);
    queryIntent.setComponent(searchActivity);
    PendingIntent pending = PendingIntent.getActivity(getContext(), 0, queryIntent,
            PendingIntent.FLAG_ONE_SHOT);

    // Now set up the bundle that will be inserted into the pending intent
    // when it's time to do the search.  We always build it here (even if empty)
    // because the voice search activity will always need to insert "QUERY" into
    // it anyway.
    Bundle queryExtras = new Bundle();

    // Now build the intent to launch the voice search.  Add all necessary
    // extras to launch the voice recognizer, and then all the necessary extras
    // to forward the results to the searchable activity
    Intent voiceIntent = new Intent(baseIntent);

    // Add all of the configuration options supplied by the searchable's metadata
    String languageModel = RecognizerIntent.LANGUAGE_MODEL_FREE_FORM;
    String prompt = null;
    String language = null;
    int maxResults = 1;

    Resources resources = getResources();
    if (searchable.getVoiceLanguageModeId() != 0) {
        languageModel = resources.getString(searchable.getVoiceLanguageModeId());
    }
    if (searchable.getVoicePromptTextId() != 0) {
        prompt = resources.getString(searchable.getVoicePromptTextId());
    }
    if (searchable.getVoiceLanguageId() != 0) {
        language = resources.getString(searchable.getVoiceLanguageId());
    }
    if (searchable.getVoiceMaxResults() != 0) {
        maxResults = searchable.getVoiceMaxResults();
    }
    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResults);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            searchActivity == null ? null : searchActivity.flattenToShortString());

    // Add the values that configure forwarding the results
    voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pending);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, queryExtras);

    return voiceIntent;
}

From source file:android.support.v7.widget.SearchView.java

/**
 * Create and return an Intent that can launch the voice search activity, perform a specific
 * voice transcription, and forward the results to the searchable activity.
 *
 * @param baseIntent The voice app search intent to start from
 * @return A completely-configured intent ready to send to the voice search activity
 *///  w  w  w . j a  v a 2  s  .co  m
@TargetApi(Build.VERSION_CODES.FROYO)
private Intent createVoiceAppSearchIntent(Intent baseIntent, SearchableInfo searchable) {
    ComponentName searchActivity = searchable.getSearchActivity();

    // create the necessary intent to set up a search-and-forward operation
    // in the voice search system.   We have to keep the bundle separate,
    // because it becomes immutable once it enters the PendingIntent
    Intent queryIntent = new Intent(Intent.ACTION_SEARCH);
    queryIntent.setComponent(searchActivity);
    PendingIntent pending = PendingIntent.getActivity(getContext(), 0, queryIntent,
            PendingIntent.FLAG_ONE_SHOT);

    // Now set up the bundle that will be inserted into the pending intent
    // when it's time to do the search.  We always build it here (even if empty)
    // because the voice search activity will always need to insert "QUERY" into
    // it anyway.
    Bundle queryExtras = new Bundle();
    if (mAppSearchData != null) {
        queryExtras.putParcelable(SearchManager.APP_DATA, mAppSearchData);
    }

    // Now build the intent to launch the voice search.  Add all necessary
    // extras to launch the voice recognizer, and then all the necessary extras
    // to forward the results to the searchable activity
    Intent voiceIntent = new Intent(baseIntent);

    // Add all of the configuration options supplied by the searchable's metadata
    String languageModel = RecognizerIntent.LANGUAGE_MODEL_FREE_FORM;
    String prompt = null;
    String language = null;
    int maxResults = 1;

    if (Build.VERSION.SDK_INT >= 8) {
        Resources resources = getResources();
        if (searchable.getVoiceLanguageModeId() != 0) {
            languageModel = resources.getString(searchable.getVoiceLanguageModeId());
        }
        if (searchable.getVoicePromptTextId() != 0) {
            prompt = resources.getString(searchable.getVoicePromptTextId());
        }
        if (searchable.getVoiceLanguageId() != 0) {
            language = resources.getString(searchable.getVoiceLanguageId());
        }
        if (searchable.getVoiceMaxResults() != 0) {
            maxResults = searchable.getVoiceMaxResults();
        }
    }
    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResults);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            searchActivity == null ? null : searchActivity.flattenToShortString());

    // Add the values that configure forwarding the results
    voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pending);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, queryExtras);

    return voiceIntent;
}

From source file:org.deviceconnect.android.manager.DConnectMessageService.java

/**
 * ??Intent??./*from   w w w.  j  a v  a  2s.c  om*/
 * @param request 
 * @param response ???
 * @return ???Intent
 */
protected Intent createResponseIntent(final Intent request, final Intent response) {
    int requestCode = request.getIntExtra(IntentDConnectMessage.EXTRA_REQUEST_CODE, ERROR_CODE);
    ComponentName cn = request.getParcelableExtra(IntentDConnectMessage.EXTRA_RECEIVER);

    Intent intent = new Intent(response);
    intent.putExtra(IntentDConnectMessage.EXTRA_REQUEST_CODE, requestCode);
    intent.putExtra(IntentDConnectMessage.EXTRA_PRODUCT, getString(R.string.app_name));
    intent.putExtra(IntentDConnectMessage.EXTRA_VERSION, DConnectUtil.getVersionName(this));

    // HMAC?
    String origin = request.getStringExtra(IntentDConnectMessage.EXTRA_ORIGIN);
    if (origin == null) {
        String accessToken = request.getStringExtra(DConnectMessage.EXTRA_ACCESS_TOKEN);
        if (accessToken != null) {
            origin = findOrigin(accessToken);
        }
    }
    if (origin != null) {
        if (mHmacManager.usesHmac(origin)) {
            String nonce = request.getStringExtra(IntentDConnectMessage.EXTRA_NONCE);
            if (nonce != null) {
                String hmac = mHmacManager.generateHmac(origin, nonce);
                if (hmac != null) {
                    intent.putExtra(IntentDConnectMessage.EXTRA_HMAC, hmac);
                }
            }
        }
    } else {
        mLogger.warning("Origin is not found.");
    }

    intent.setComponent(cn);
    return intent;
}

From source file:org.moire.ultrasonic.service.DownloadServiceImpl.java

@SuppressLint("NewApi")
public void setUpRemoteControlClient() {
    if (!Util.isLockScreenEnabled(this))
        return;//  w w w. j a  v a  2  s.  co  m

    ComponentName componentName = new ComponentName(getPackageName(),
            MediaButtonIntentReceiver.class.getName());

    if (remoteControlClient == null) {
        final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(componentName);
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, mediaButtonIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteControlClient = new RemoteControlClient(broadcast);
        audioManager.registerRemoteControlClient(remoteControlClient);

        // Flags for the media transport control that this client supports.
        int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
                | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_STOP;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            flags |= RemoteControlClient.FLAG_KEY_MEDIA_POSITION_UPDATE;

            remoteControlClient
                    .setOnGetPlaybackPositionListener(new RemoteControlClient.OnGetPlaybackPositionListener() {
                        @Override
                        public long onGetPlaybackPosition() {
                            return mediaPlayer.getCurrentPosition();
                        }
                    });

            remoteControlClient.setPlaybackPositionUpdateListener(
                    new RemoteControlClient.OnPlaybackPositionUpdateListener() {
                        @Override
                        public void onPlaybackPositionUpdate(long newPositionMs) {
                            seekTo((int) newPositionMs);
                        }
                    });
        }

        remoteControlClient.setTransportControlFlags(flags);
    }
}

From source file:com.firefly.sample.castcompanionlibrary.cast.VideoCastManager.java

@SuppressLint("InlinedApi")
private void setUpRemoteControl(final MediaInfo info) {
    if (!isFeatureEnabled(BaseCastManager.FEATURE_LOCKSCREEN)) {
        return;/*from  w w  w. java 2  s .  c om*/
    }
    LOGD(TAG, "setupRemoteControl() was called");
    mAudioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);

    ComponentName eventReceiver = new ComponentName(mContext, VideoIntentReceiver.class.getName());
    mAudioManager.registerMediaButtonEventReceiver(eventReceiver);

    if (mRemoteControlClientCompat == null) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        intent.setComponent(mMediaButtonReceiverComponent);
        mRemoteControlClientCompat = new RemoteControlClientCompat(
                PendingIntent.getBroadcast(mContext, 0, intent, 0));
        RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat);
    }
    mRemoteControlClientCompat.addToMediaRouter(mMediaRouter);
    mRemoteControlClientCompat.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE);
    if (null == info) {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
        return;
    } else {
        mRemoteControlClientCompat.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
    }

    // Update the remote control's image
    updateLockScreenImage(info);

    // update the remote control's metadata
    updateLockScreenMetadata();
}

From source file:com.example.navigationsearchview.NavigationSearchView.java

/**
 * Create and return an Intent that can launch the voice search activity,
 * perform a specific voice transcription, and forward the results to the
 * searchable activity.//  w w  w . j av  a2 s. c o m
 *
 * @param baseIntent
 *            The voice app search intent to start from
 * @return A completely-configured intent ready to send to the voice search
 *         activity
 */
@TargetApi(Build.VERSION_CODES.FROYO)
private Intent createVoiceAppSearchIntent(Intent baseIntent, SearchableInfo searchable) {
    ComponentName searchActivity = searchable.getSearchActivity();

    // create the necessary intent to set up a search-and-forward operation
    // in the voice search system. We have to keep the bundle separate,
    // because it becomes immutable once it enters the PendingIntent
    Intent queryIntent = new Intent(Intent.ACTION_SEARCH);
    queryIntent.setComponent(searchActivity);
    PendingIntent pending = PendingIntent.getActivity(getContext(), 0, queryIntent,
            PendingIntent.FLAG_ONE_SHOT);

    // Now set up the bundle that will be inserted into the pending intent
    // when it's time to do the search. We always build it here (even if
    // empty)
    // because the voice search activity will always need to insert "QUERY"
    // into
    // it anyway.
    Bundle queryExtras = new Bundle();
    if (mAppSearchData != null) {
        queryExtras.putParcelable(SearchManager.APP_DATA, mAppSearchData);
    }

    // Now build the intent to launch the voice search. Add all necessary
    // extras to launch the voice recognizer, and then all the necessary
    // extras
    // to forward the results to the searchable activity
    Intent voiceIntent = new Intent(baseIntent);

    // Add all of the configuration options supplied by the searchable's
    // metadata
    String languageModel = RecognizerIntent.LANGUAGE_MODEL_FREE_FORM;
    String prompt = null;
    String language = null;
    int maxResults = 1;

    if (Build.VERSION.SDK_INT >= 8) {
        Resources resources = getResources();
        if (searchable.getVoiceLanguageModeId() != 0) {
            languageModel = resources.getString(searchable.getVoiceLanguageModeId());
        }
        if (searchable.getVoicePromptTextId() != 0) {
            prompt = resources.getString(searchable.getVoicePromptTextId());
        }
        if (searchable.getVoiceLanguageId() != 0) {
            language = resources.getString(searchable.getVoiceLanguageId());
        }
        if (searchable.getVoiceMaxResults() != 0) {
            maxResults = searchable.getVoiceMaxResults();
        }
    }
    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, languageModel);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, prompt);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, language);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, maxResults);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            searchActivity == null ? null : searchActivity.flattenToShortString());

    // Add the values that configure forwarding the results
    voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pending);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT_BUNDLE, queryExtras);

    return voiceIntent;
}

From source file:org.path.episample.android.activities.MainMenuActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    /*/*www  .ja  v a2  s .c o m*/
     * if (item.getItemId() == MENU_FILL_FORM) {
      swapToFragmentView(ScreenList.FORM_CHOOSER); return true; } else 
     */

    if (item.getItemId() == MENU_MAIN_MENU) {
        swapToFragmentView(ScreenList.MAIN_MENU);

        return true;
    } else if (item.getItemId() == MENU_PULL_FORMS) {
        swapToFragmentView(ScreenList.FORM_DOWNLOADER);
        return true;
    } else if (item.getItemId() == MENU_CLOUD_FORMS) {
        try {
            Intent syncIntent = new Intent();
            syncIntent.setComponent(
                    new ComponentName("org.path.sync", "org.opendatakit.sync.activities.SyncActivity"));
            syncIntent.setAction(Intent.ACTION_DEFAULT);
            Bundle bundle = new Bundle();
            bundle.putString(APP_NAME, appName);
            syncIntent.putExtras(bundle);
            this.startActivityForResult(syncIntent, SYNC_ACTIVITY_CODE);
        } catch (ActivityNotFoundException e) {
            WebLogger.getLogger(getAppName()).printStackTrace(e);
            Toast.makeText(this, R.string.sync_not_found, Toast.LENGTH_LONG).show();
        }
        return true;
    } else if (item.getItemId() == MENU_MANAGE_FORMS) {
        swapToFragmentView(ScreenList.FORM_DELETER);
        return true;
    } else if (item.getItemId() == MENU_EDIT_INSTANCE) {
        swapToFragmentView(ScreenList.WEBKIT);
        return true;
    } else if (item.getItemId() == MENU_PUSH_FORMS) {
        swapToFragmentView(ScreenList.INSTANCE_UPLOADER_TABLE_CHOOSER);
        return true;
    } else if (item.getItemId() == MENU_PREFERENCES) {
        // PreferenceFragment missing from support library...
        Intent ig = new Intent(this, PreferencesActivity.class);
        // TODO: convert this activity into a preferences fragment
        ig.putExtra(APP_NAME, getAppName());
        startActivity(ig);
        return true;
    } else if (item.getItemId() == MENU_ADMIN_PREFERENCES) {
        String pw = PropertiesSingleton.getProperty(appName, AdminPreferencesActivity.KEY_ADMIN_PW);
        if (pw == null || "".equalsIgnoreCase(pw)) {
            Intent i = new Intent(getApplicationContext(), AdminPreferencesActivity.class);
            // TODO: convert this activity into a preferences fragment
            i.putExtra(APP_NAME, getAppName());
            startActivity(i);
        } else {
            createPasswordDialog();
        }
        return true;
    } else if (item.getItemId() == MENU_BACKUP_CENSUS) {
        if (CensusUtil.getCount(this) == 0) {
            Toast.makeText(this, getString(R.string.census_db_empty), Toast.LENGTH_LONG).show();
        } else {
            BackupRestoreUtils.BackupResult result = BackupRestoreUtils.simpleBackup(this, getAppName());
            if (result.getSuccess() == true) {
                Toast.makeText(this, getString(R.string.backup_successful, result.getDatabaseFileName()),
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, getString(R.string.backup_unsuccessful, result.getDatabaseFileName()),
                        Toast.LENGTH_LONG).show();
            }
        }
        return true;
    } else if (item.getItemId() == MENU_RESTORE_CENSUS) {
        swapToFragmentView(ScreenList.RESTORE_MODULE);
        return true;
    } else if (item.getItemId() == MENU_EDIT_CENSUS) {
        BackupRestoreUtils.backupCensus(Survey.getInstance().getApplicationContext(), "survey");
        swapToFragmentView(ScreenList.EDIT_CENSUS_MODULE);
        return true;
    } else if (item.getItemId() == MENU_REMOVE_CENSUS) {
        swapToFragmentView(ScreenList.REMOVE_CENSUS_MODULE);
        return true;
    } else if (item.getItemId() == MENU_MARK_CENSUS_AS_INVALID) {
        swapToFragmentView(ScreenList.INVALIDATE_CENSUS_MODULE);
        return true;
    } /*else if(item.getItemId() == MENU_SEND_REVEIVE_BLUETOOTH) {
      swapToFragmentView(ScreenList.SEND_RECEIVE_BLUETOOTH_MODULE);
      return true;
      }*/ else if (item.getItemId() == MENU_ABOUT) {
        swapToFragmentView(ScreenList.ABOUT_MENU);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

From source file:me.tb.player.SkeletonActivity.java

public void onShareClick() {
    Resources resources = getResources();
    String type = "image/*";
    String mediaPath = Environment.getExternalStorageDirectory() + "/game_icon1.png";

    // Create the URI from the media
    File media = new File(mediaPath);
    Uri uri = Uri.fromFile(media);/*from  w w  w. ja  v  a 2s.  c o m*/

    Intent emailIntent = new Intent();
    emailIntent.setAction(Intent.ACTION_SEND);
    // Native email client doesn't currently support HTML, but it doesn't hurt to try in case they fix it
    emailIntent.putExtra(Intent.EXTRA_TEXT,
            "Download in Google Play Store\nhttps://play.google.com/store/apps/details?id=me.tb.player");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Play Word Bandit - Multiplayer");
    emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
    emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    emailIntent.setType(type);

    PackageManager pm = getPackageManager();
    Intent sendIntent = new Intent(Intent.ACTION_SEND);
    sendIntent.setType(type);

    Intent openInChooser = Intent.createChooser(emailIntent, resources.getString(R.string.share_chooser_text));

    List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
    List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
    for (int i = 0; i < resInfo.size(); i++) {
        // Extract the label, append it, and repackage it in a LabeledIntent
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;
        if (packageName.contains("com.google.android.gm")) {
            emailIntent.setPackage(packageName);
        } else if (packageName.contains("twitter") || packageName.contains("facebook.katana")
                || packageName.contains("com.instagram.android")) {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
            intent.setAction(Intent.ACTION_SEND);
            intent.setType(type);
            // Add the URI and the caption to the Intent.
            intent.putExtra(Intent.EXTRA_STREAM, uri);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            if (packageName.contains("twitter") || packageName.contains("instagram")) {
                intent.putExtra(Intent.EXTRA_TEXT, shareMessageCombo
                        + "\nDownload now https://play.google.com/store/apps/details?id=me.tb.player");
            }
            intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
        }
    }

    // convert intentList to array
    LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);

    openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
    startActivity(openInChooser);
}

From source file:org.mixare.MixViewActivity.java

public void setErrorDialog(int error) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(false);/*  w  w w .  j a v  a2s. c  om*/
    switch (error) {
    case NO_NETWORK_ERROR:
        builder.setMessage(getString(R.string.connection_error_dialog));
        break;
    case GPS_ERROR:
        builder.setMessage(getString(R.string.gps_error_dialog));
        break;
    case GENERAL_ERROR:
        builder.setMessage(getString(R.string.general_error_dialog));
        break;
    case UNSUPPORTED_HARDWARE:
        builder.setMessage(getString(R.string.unsupportet_hardware_dialog));
        break;
    }

    /*Retry*/
    builder.setPositiveButton(R.string.connection_error_dialog_button1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // "restart" mixare
            startActivity(
                    new Intent(MixContext.getInstance().getApplicationContext(), PluginLoaderActivity.class));
            finish();
        }
    });
    if (error == GPS_ERROR) {
        /* Open settings */
        builder.setNeutralButton(R.string.connection_error_dialog_button2,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        try {
                            Intent intent1 = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivityForResult(intent1, 42);
                        } catch (Exception e) {
                            Log.d(Config.TAG, "No Location Settings");
                        }
                    }
                });
    } else if (error == NO_NETWORK_ERROR) {
        builder.setNeutralButton(R.string.connection_error_dialog_button2,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        try {
                            Intent intent1 = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
                            ComponentName cName = new ComponentName("com.android.phone",
                                    "com.android.phone.Settings");
                            intent1.setComponent(cName);
                            startActivityForResult(intent1, 42);
                        } catch (Exception e) {
                            Log.d(Config.TAG, "No Network Settings");
                        }
                    }
                });
    }
    /*Close application*/
    builder.setNegativeButton(R.string.connection_error_dialog_button3, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            finish();
        }
    });

    AlertDialog alert = builder.create();
    alert.show();
}