如何自定义共享意图的内置选择器(对话框)?

时间:2015-11-03 06:16:59

标签: android android-intent

有没有办法在createChooser中自定义共享意图的startActivity(Intent.createChooser(i,"Share via"));? 显示不在对话框中的应用程序可能在滚动视图中作为按钮

Built in chooser

custom chooser

3 个答案:

答案 0 :(得分:1)

字符串urlToShare =" www.google.com"

code to share link twitter

try {
        Intent shareIntent = ShareCompat.IntentBuilder.from(getParent())
                .setType("text/plain")
                .setText("Sharing text with image link \n "+urlToShare).setStream(null)
                .getIntent()
                .setPackage("com.twitter.android");
        startActivity(shareIntent);
    } catch (Exception e) {
        // If we failed (not native FB app installed), try share through SEND
        Intent intent = new Intent(Intent.ACTION_SEND);
        String sharerUrl = "https://twitter.com/intent/tweet?text=" + urlToShare;
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
        startActivity(intent);
    } 

code to share link google plus

try {
        Intent shareIntent = ShareCompat.IntentBuilder.from(getParent())
                .setType("text/plain")
                .setText("Sharing text with image link \n "+urlToShare).setStream(null)
                .getIntent()
                .setPackage("com.google.android.apps.plus");
        startActivity(shareIntent);
    } catch (Exception e) {
        // If we failed (not native FB app installed), try share through SEND
        Intent intent = new Intent(Intent.ACTION_SEND);
        String sharerUrl = "https://plus.google.com/share?url=" + urlToShare;
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
        startActivity(intent);
    }

code to share link whatsapp

Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
    intent.setType("text/plain");
    intent.setPackage("com.whatsapp");
    startActivity(intent);

答案 1 :(得分:0)

这就是我所做的就是将链接和图像分享给其他应用程序,只需尝试

 /*
 * Method to share either text or URL.
 */
private void shareTextUrl() {
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("text/plain");
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

    // Add data to the intent, the receiving app will decide
    // what to do with it.
    share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
    share.putExtra(Intent.EXTRA_TEXT, "http://www.google.com");

    startActivity(Intent.createChooser(share, "Share link!"));
}

/*
 * Method to share any image.
 */
private void shareImage() {
    Intent share = new Intent(Intent.ACTION_SEND);

    // If you want to share a png image only, you can do:
    // setType("image/png"); OR for jpeg: setType("image/jpeg");
    share.setType("image/*");

    // Make sure you put example png image named myImage.png in your
    // directory
    String imagePath = Environment.getExternalStorageDirectory()
            + "/myImage.png";

    File imageFileToShare = new File(imagePath);

    Uri uri = Uri.fromFile(imageFileToShare);
    share.putExtra(Intent.EXTRA_STREAM, uri);

    startActivity(Intent.createChooser(share, "Share Image!"));
}

答案 2 :(得分:0)

是的,你可以通过将共享选项作为活动并将它们传递给适配器来实现,我发布了一个示例代码

Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List activities = this.getPackageManager().queryIntentActivities(sendIntent, 0);

通过这个你将获得共享Intents in activities对象,然后你可以将它转换为Object Array这样的东西

Objects[] item = activities.toArray();

for( int i=0; i<item.length; i++ ) {

    ResolveInfo infoName = (ResolveInfo) items[i];
    String name =  info.activityInfo.name;
    Drawable logo = info.loadIcon(context.getPackageManager());

    // Set them to your Views
}

当您单击视图并且想要执行共享功能时,您将执行类似这样的操作

ResolveInfo info = (ResolveInfo) item(position);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Your Text");
startActivity(intent);