How to force Share Intent to open a specific app?

谁都会走 提交于 2019-12-17 02:35:10

问题


I like share intent, it is perfect to open sharing apps with image and text parameters.

But now i'm researching the way to force share intent to open a specific app from the list, with the paramters given to the share intent.

This is my actual code, it shows the list of sharing apps installed on the phone. Please, can somedone tell me what i should add to the code to force for example official twitter app? and official faccebok app?

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("file:///sdcard/test.jpg");  
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "body text");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));

Thanks


回答1:


For Facebook

public void shareFacebook() {
        String fullUrl = "https://m.facebook.com/sharer.php?u=..";
        try {
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setClassName("com.facebook.katana",
                    "com.facebook.katana.ShareLinkActivity");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, "your title text");
            startActivity(sharingIntent);

        } catch (Exception e) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(fullUrl));
            startActivity(i);

        }
    }

For Twitter.

public void shareTwitter() {
        String message = "Your message to post";
        try {
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setClassName("com.twitter.android","com.twitter.android.PostActivity");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, message);
            startActivity(sharingIntent);
        } catch (Exception e) {
            Log.e("In Exception", "Comes here");
            Intent i = new Intent();
            i.putExtra(Intent.EXTRA_TEXT, message);
            i.setAction(Intent.ACTION_VIEW);
            i.setData(Uri.parse("https://mobile.twitter.com/compose/tweet"));
            startActivity(i);
        }
    }



回答2:


There is a way more generic to do that, and is not necessary to know the full package name of the application intent.

See this post: How to customize share intent in Android?




回答3:


100% Working solution

if you want to share something in any app you want or open a url via every action, just use this method:

private void shareOrViewUrlViaThisApp(String appPackageName, String url) {
    boolean found = false;
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));

    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
            if (info.activityInfo.packageName.toLowerCase().contains(appPackageName) ||
                    info.activityInfo.name.toLowerCase().contains(appPackageName) ) {

                intent.setPackage(info.activityInfo.packageName);
                found = true;
                break;
            }
        }
        if (!found)
            return;

        startActivity(Intent.createChooser(intent, "Select"));
    }
}

and simply call:

shareOrViewUrlViaThisApp(<your package name>,<your url>);

This answer was inspired from this.



来源:https://stackoverflow.com/questions/8308666/how-to-force-share-intent-to-open-a-specific-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!