Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. String AppURL = "https://play.google.com/store/apps/details?id=" + getApplicationContext().getPackageName();
  2. String urlToShare = "http://stackoverflow.com/questions/7545254"; // I need custom url to specific part in my app
  3.  
  4. // See if official Facebook app is found
  5. boolean facebookAppFound = false;
  6. List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0);
  7. for (ResolveInfo info : matches) {
  8. if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) {
  9. intent.setPackage(info.activityInfo.packageName);
  10. facebookAppFound = true;
  11. break;
  12. }
  13. }
  14.  
  15. // As fallback, launch sharer.php in a browser
  16. if (!facebookAppFound) {
  17. String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare;
  18. intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
  19. }else{
  20.  
  21. Intent intent = new Intent(Intent.ACTION_SEND);
  22. intent.setType("text/plain");
  23. // intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect!
  24. intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
  25. }
  26.  
  27. startActivity(intent);
  28.  
  29. ShareDialog shareDialog;
  30. FacebookSdk.sdkInitialize(Activity.this);
  31. shareDialog = new ShareDialog(act);
  32. ShareLinkContent linkContent = new ShareLinkContent.Builder()
  33. .setContentTitle("title")
  34. .setContentDescription(
  35. "Description")
  36. .setContentUrl(Uri.parse("your url")).build();
  37. shareDialog.show(linkContent);
  38.  
  39. private void shareFacebook(File mFileImagePath) {
  40. String application = "com.facebook.katana";
  41. boolean installed = checkAppInstall(application);
  42. if (installed) {
  43. Intent mIntentShare = new Intent(Intent.ACTION_SEND);
  44. String mStrExtension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(mFileImagePath).toString());
  45. String mStrMimeType = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(mStrExtension);
  46. if (mStrExtension.equalsIgnoreCase("") || mStrMimeType == null) {
  47. // if there is no extension or there is no definite mimetype, still try to open the file
  48. mIntentShare.setType("text*//*");
  49. } else {
  50. mIntentShare.setType(mStrMimeType);
  51. }
  52. mIntentShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mFileImagePath));
  53. mIntentShare.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  54.  
  55. mIntentShare.setPackage(application);
  56. startActivity(mIntentShare);
  57. } else {
  58.  
  59. Toast.makeText(mContext, "Facebook have not been installed.", Toast.LENGTH_SHORT).show();
  60. }
  61.  
  62. }
  63.  
  64. private boolean checkAppInstall(String uri) {
  65. PackageManager pm = getPackageManager();
  66. try {
  67. pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
  68. return true;
  69. } catch (PackageManager.NameNotFoundException e) {
  70. //Error
  71. }
  72. return false;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement