Example usage for android.content Intent resolveActivity

List of usage examples for android.content Intent resolveActivity

Introduction

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

Prototype

public ComponentName resolveActivity(@NonNull PackageManager pm) 

Source Link

Document

Return the Activity component that should be used to handle this intent.

Usage

From source file:com.anjalimacwan.fragment.NoteEditFragment.java

public void dispatchKeyShortcutEvent(int keyCode) {
    switch (keyCode) {

    // CTRL+S: Save
    case KeyEvent.KEYCODE_S:
        // Set current note contents to a String
        contents = noteContents.getText().toString();

        // If EditText is empty, show toast informing user to enter some text
        if (contents.equals(""))
            showToast(R.string.empty_note);
        else {/*from ww w  .j a v  a 2  s.  c om*/
            try {
                // Keyboard shortcut just saves the note; no dialog shown
                saveNote();
                isSavedNote = true;

                // Change window title
                String title;
                try {
                    title = listener.loadNoteTitle(filename);
                } catch (IOException e) {
                    title = getResources().getString(R.string.edit_note);
                }

                getActivity().setTitle(title);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(title,
                            null, ContextCompat.getColor(getActivity(), R.color.primary));
                    getActivity().setTaskDescription(taskDescription);
                }
            } catch (IOException e) {
                // Show error message as toast if file fails to save
                showToast(R.string.failed_to_save);
            }
        }
        break;

    // CTRL+D: Delete
    case KeyEvent.KEYCODE_D:
        listener.showDeleteDialog();
        break;

    // CTRL+H: Share
    case KeyEvent.KEYCODE_H:
        // Set current note contents to a String
        contents = noteContents.getText().toString();

        // If EditText is empty, show toast informing user to enter some text
        if (contents.equals(""))
            showToast(R.string.empty_note);
        else {
            // Send a share intent
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_TEXT, contents);
            shareIntent.setType("text/plain");

            // Verify that the intent will resolve to an activity, and send
            if (shareIntent.resolveActivity(getActivity().getPackageManager()) != null)
                startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
        }
        break;
    }
}

From source file:com.notepadlite.NoteEditFragment.java

public void dispatchKeyShortcutEvent(int keyCode) {
    switch (keyCode) {

    // CTRL+S: Save
    case KeyEvent.KEYCODE_S:
        // Set current note contents to a String
        contents = noteContents.getText().toString();

        // If EditText is empty, show toast informing user to enter some text
        if (contents.equals(""))
            showToast(R.string.empty_note);
        else {/* w w  w.  j a  v  a 2  s. co  m*/
            try {
                // Keyboard shortcut just saves the note; no dialog shown
                saveNote();
                isSavedNote = true;

                // Change window title
                String title;
                try {
                    title = listener.loadNoteTitle(filename);
                } catch (IOException e) {
                    title = getResources().getString(R.string.edit_note);
                }

                getActivity().setTitle(title);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ActivityManager.TaskDescription taskDescription = new ActivityManager.TaskDescription(title,
                            null, getResources().getColor(R.color.primary));
                    getActivity().setTaskDescription(taskDescription);
                }
            } catch (IOException e) {
                // Show error message as toast if file fails to save
                showToast(R.string.failed_to_save);
            }
        }
        break;

    // CTRL+D: Delete
    case KeyEvent.KEYCODE_D:
        listener.showDeleteDialog();
        break;

    // CTRL+H: Share
    case KeyEvent.KEYCODE_H:
        // Set current note contents to a String
        contents = noteContents.getText().toString();

        // If EditText is empty, show toast informing user to enter some text
        if (contents.equals(""))
            showToast(R.string.empty_note);
        else {
            // Send a share intent
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_TEXT, contents);
            shareIntent.setType("text/plain");

            // Verify that the intent will resolve to an activity, and send
            if (shareIntent.resolveActivity(getActivity().getPackageManager()) != null)
                startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
        }
        break;
    }
}

From source file:org.easyrpg.player.player.EasyRpgPlayerActivity.java

private void reportBug() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle(R.string.app_name);

    final SpannableString bug_msg = new SpannableString(
            getApplicationContext().getString(R.string.report_bug_msg));
    Linkify.addLinks(bug_msg, Linkify.ALL);

    // set dialog message
    alertDialogBuilder.setMessage(bug_msg).setCancelable(false)
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    ArrayList<Uri> files = new ArrayList<Uri>();
                    String savepath = getIntent().getStringExtra(TAG_SAVE_PATH);
                    files.add(Uri.fromFile(new File(savepath + "/easyrpg_log.txt")));
                    for (File f : GameBrowserHelper.getSavegames(new File(savepath))) {
                        files.add(Uri.fromFile(f));
                    }/*from w  w w  .jav a  2s.c o  m*/

                    if (Build.VERSION.SDK_INT >= 24) {
                        // Lazy workaround as suggested on https://stackoverflow.com/q/38200282
                        try {
                            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                            m.invoke(null);
                        } catch (Exception e) {
                            Log.i("EasyRPG", "Bug report: Calling disableDeathOnFileUriExposure failed");
                        }
                    }

                    Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                    intent.setData(Uri.parse("mailto:"));
                    intent.setType("*/*");
                    intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "easyrpg@easyrpg.org" });
                    intent.putExtra(Intent.EXTRA_SUBJECT, "Bug report");
                    intent.putExtra(Intent.EXTRA_TEXT,
                            getApplicationContext().getString(R.string.report_bug_mail));
                    intent.putExtra(Intent.EXTRA_STREAM, files);
                    if (intent.resolveActivity(getPackageManager()) != null) {
                        startActivity(intent);
                    }
                }
            }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    AlertDialog alertDialog = alertDialogBuilder.create();

    alertDialog.show();

    ((TextView) alertDialog.findViewById(android.R.id.message))
            .setMovementMethod(LinkMovementMethod.getInstance());
}

From source file:com.cm.android.beercellar.ui.ImageGridFragment.java

/**
 * @author anshu/*from  w ww. ja  v a2s . c om*/
 */
public void takePhoto() throws IOException {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = Utils.createImageFile(getActivity());
        } catch (IOException ex) {
            // Error occurred while creating the File
            Log.e("takePhoto", ex.toString());
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            getActivity().getSharedPreferences(Utils.SHARED_PREF_NAME, Context.MODE_PRIVATE).edit()
                    .putString("IMAGE_NAME", photoFile.getName()).commit();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

From source file:com.master.metehan.filtereagle.ActivityMain.java

private void menu_about() {
    // Create view
    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.about, null, false);
    TextView tvVersionName = (TextView) view.findViewById(R.id.tvVersionName);
    TextView tvVersionCode = (TextView) view.findViewById(R.id.tvVersionCode);
    Button btnRate = (Button) view.findViewById(R.id.btnRate);
    TextView tvLicense = (TextView) view.findViewById(R.id.tvLicense);

    // Show version
    tvVersionName.setText(Util.getSelfVersionName(this));
    if (!Util.hasValidFingerprint(this))
        tvVersionName.setTextColor(Color.GRAY);
    tvVersionCode.setText(Integer.toString(Util.getSelfVersionCode(this)));

    // Handle license
    tvLicense.setMovementMethod(LinkMovementMethod.getInstance());

    // Handle logcat
    view.setOnClickListener(new View.OnClickListener() {
        private short tap = 0;
        private Toast toast = Toast.makeText(ActivityMain.this, "", Toast.LENGTH_SHORT);

        @Override/*from  ww  w  .  j av  a2  s  .  co  m*/
        public void onClick(View view) {
            tap++;
            if (tap == 7) {
                tap = 0;
                toast.cancel();

                Intent intent = getIntentLogcat();
                if (intent.resolveActivity(getPackageManager()) != null)
                    startActivityForResult(intent, REQUEST_LOGCAT);

            } else if (tap > 3) {
                toast.setText(Integer.toString(7 - tap));
                toast.show();
            }
        }
    });

    // Handle rate
    btnRate.setVisibility(
            getIntentRate(this).resolveActivity(getPackageManager()) == null ? View.GONE : View.VISIBLE);
    btnRate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(getIntentRate(ActivityMain.this));
        }
    });

    // Show dialog
    dialogAbout = new AlertDialog.Builder(this).setView(view).setCancelable(true)
            .setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                    dialogAbout = null;
                }
            }).create();
    dialogAbout.show();
}

From source file:com.notepadlite.NoteEditFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Hide soft keyboard
    EditText editText = (EditText) getActivity().findViewById(R.id.editText1);
    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

    switch (item.getItemId()) {
    case android.R.id.home:
        // Override default Android "up" behavior to instead mimic the back button
        getActivity().onBackPressed();/*from  www.j  av  a 2 s.c  o m*/
        return true;

    // Save button
    case R.id.action_save:
        // Get current note contents from EditText
        noteContents = (EditText) getActivity().findViewById(R.id.editText1);
        contents = noteContents.getText().toString();

        // If EditText is empty, show toast informing user to enter some text
        if (contents.equals(""))
            showToast(R.string.empty_note);
        else if (directEdit)
            getActivity().onBackPressed();
        else {
            // If no changes were made since last save, switch back to NoteViewFragment
            if (contentsOnLoad.equals(noteContents.getText().toString())) {
                Bundle bundle = new Bundle();
                bundle.putString("filename", filename);

                Fragment fragment = new NoteViewFragment();
                fragment.setArguments(bundle);

                getFragmentManager().beginTransaction().replace(R.id.noteViewEdit, fragment, "NoteViewFragment")
                        .commit();
            } else {
                SharedPreferences pref = getActivity().getSharedPreferences(
                        getActivity().getPackageName() + "_preferences", Context.MODE_PRIVATE);
                if (pref.getBoolean("show_dialogs", true)) {
                    // Show save button dialog
                    listener.showSaveButtonDialog();
                } else {
                    try {
                        saveNote();

                        if (listener.isShareIntent())
                            getActivity().finish();
                        else {
                            Bundle bundle = new Bundle();
                            bundle.putString("filename", filename);

                            Fragment fragment = new NoteViewFragment();
                            fragment.setArguments(bundle);

                            getFragmentManager().beginTransaction()
                                    .replace(R.id.noteViewEdit, fragment, "NoteViewFragment").commit();
                        }
                    } catch (IOException e) {
                        // Show error message as toast if file fails to save
                        showToast(R.string.failed_to_save);
                    }
                }
            }
        }
        return true;

    // Delete button
    case R.id.action_delete:
        listener.showDeleteDialog();
        return true;

    // Share menu item
    case R.id.action_share:
        // Set current note contents to a String
        contents = noteContents.getText().toString();

        // If EditText is empty, show toast informing user to enter some text
        if (contents.equals(""))
            showToast(R.string.empty_note);
        else {
            // Send a share intent
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_TEXT, contents);
            intent.setType("text/plain");

            // Verify that the intent will resolve to an activity, and send
            if (intent.resolveActivity(getActivity().getPackageManager()) != null)
                startActivity(Intent.createChooser(intent, getResources().getText(R.string.send_to)));
        }

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

From source file:tk.eatheat.omnisnitch.ui.SwitchLayout.java

private void startApplicationDetailsActivity(String packageName) {
    Intent hideRecent = new Intent(SwitchService.RecentsReceiver.ACTION_HIDE_OVERLAY);
    mContext.sendBroadcast(hideRecent);//from ww  w .  j  a va 2s.  c om

    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
            Uri.fromParts("package", packageName, null));
    intent.setComponent(intent.resolveActivity(mContext.getPackageManager()));
    TaskStackBuilder.create(mContext).addNextIntentWithParentStack(intent).startActivities();
}

From source file:com.buddi.client.dfu.DfuActivity.java

private void openFileChooser() {
    final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType(mFileTypeTmp == DfuService.TYPE_AUTO ? DfuService.MIME_TYPE_ZIP : DfuService.MIME_TYPE_HEX);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        // file browser has been found on the device
        startActivityForResult(intent, SELECT_FILE_REQ);
    } else {//from  w w w . j a  v a  2 s.  c  om
        // there is no any file browser app, let's try to download one
        final View customView = getLayoutInflater().inflate(R.layout.app_file_browser, null);
        final ListView appsList = (ListView) customView.findViewById(android.R.id.list);
        appsList.setAdapter(new FileBrowserAppsAdapter(this));
        appsList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        appsList.setItemChecked(0, true);
        new AlertDialog.Builder(this).setTitle(R.string.dfu_alert_no_filebrowser_title).setView(customView)
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog, final int which) {
                        dialog.dismiss();
                    }
                }).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog, final int which) {
                        final int pos = appsList.getCheckedItemPosition();
                        if (pos >= 0) {
                            final String query = getResources()
                                    .getStringArray(R.array.dfu_app_file_browser_action)[pos];
                            final Intent storeIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(query));
                            startActivity(storeIntent);
                        }
                    }
                }).show();
    }
}

From source file:me.albertonicoletti.latex.activities.EditorActivity.java

private void openResultingFile(Header header, File file) {
    final String outputFolderPath = PreferenceHelper.getOutputFolder(getApplicationContext());
    final String headerType = header.getValue();
    // If it's a PDF, the compile succeeded
    if (headerType.equals("application/pdf") || headerType.equals("application/x-dvi")) {
        String ext = headerType.substring(headerType.length() - 3, headerType.length());
        // Saves the file in the output directory and tries to open it
        byte[] bytes = FilesUtils.readBinaryFile(file);
        String pdfName = document.getName().substring(0, document.getName().lastIndexOf(".") + 1) + ext;
        File pdf = new File(outputFolderPath, pdfName);
        FilesUtils.writeBinaryFile(pdf, bytes);
        Intent pdfIntent = new Intent();
        pdfIntent.setAction(Intent.ACTION_VIEW);
        pdfIntent.setDataAndType(Uri.fromFile(pdf), headerType);

        if (pdfIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(pdfIntent);/*  w  ww  .  j  a v a 2s .c om*/
        } else {
            Toast.makeText(getApplicationContext(), "You don't have any " + ext + " reader!", Toast.LENGTH_LONG)
                    .show();
        }
    } else {
        final Document receivedDocument = new Document(file);
        // Asks the user if he wishes to open the log.
        DialogsUtil.showConfirmDialog(EditorActivity.this, getString(R.string.compiling_error_title),
                getString(R.string.compiling_error_message), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                }, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        receivedDocument.setLog();
                        openDocumentInEditor(receivedDocument);
                    }
                });
    }
}

From source file:com.example.android.navigationdrawerexample.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }//from  ww w  .ja  va  2s .  c  o m
    // Handle action buttons
    switch (item.getItemId()) {
    case R.id.action_websearch:
        // create intent to perform web search for this planet
        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
        // catch event that there's no activity to handle intent
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            makeText(this, R.string.app_not_available, LENGTH_LONG).show();
        }
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}