Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. /**
  2. * Util function for opening activity in other app via <code>uri</code>.
  3. * If there is no app which can handle this uri, this method will show toast error message to
  4. * user and won't attempt to start activity (which would result in ActivityNotFoundException).
  5. */
  6. public static void openInOtherApp(@NonNull Context context, @NonNull Uri uri) {
  7. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  8.  
  9. /* VERIFY THERE IS APP THAT CAN HANDLE THIS INTENT */
  10. List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent, 0);
  11. boolean isSafeIntent = activities.size() > 0;
  12.  
  13. if (isSafeIntent) {
  14. context.startActivity(intent);
  15. } else {
  16. Toast.makeText(context, R.string.no_app_can_handle_intent, Toast.LENGTH_SHORT).show();
  17. }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement