【问题标题】:Share intent location共享意图位置
【发布时间】:2015-04-28 03:40:14
【问题描述】:

我一直在尝试通过 whatsapp 共享意图发送位置,到目前为止我的代码:

String uri = "geo:" + currentLoc.getLatitude() + "," +currentLoc.getLongitude() + "?q=" + currentLoc.getLatitude() + "," + currentLoc.getLongitude();
            Intent i=new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
            startActivity(i);

问题是当应用程序选择器打开时,它只显示“谷歌地图”和“位智”。如何修改它,以便我可以通过许多其他支持位置的应用(如 whatsapp 或 facebook)发送它?

【问题讨论】:

  • 告诉我this 是否适合您?

标签: android android-intent location


【解决方案1】:

您需要将android.content.Intent.ACTION_VIEW 替换为Intent.ACTION_SENDIntent.ACTION_SEND_MULTIPLE

您可以在应用程序here 之间找到有关sendingreceiving 数据的完整信息。

最后,你需要像这样打开Intent

startActivity(Intent.createChooser(shareIntent, "Share via"));

而不是:startActivity(shareIntent); 解释为here。这将创建一个对话框供用户选择要用于共享的应用程序。


示例代码:

String uri = "http://maps.google.com/maps?saddr=" + location.latitude + "," + location.longitude;

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, uri);
startActivity(Intent.createChooser(sharingIntent, "Share in..."));

【讨论】:

    【解决方案2】:

    尝试使用下面的代码,这是官方文档中的示例。

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my location to send.");
    sendIntent.setType("text/plain");
    startActivity(sendIntent);
    
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.share, menu);
        MenuItem item = menu.findItem(R.id.share_item);
        actionProvider = (ShareActionProvider) item.getActionProvider();
    
        // Create the share Intent
        String shareText = URL_TO_SHARE;
        Intent shareIntent = ShareCompat.IntentBuilder.from(this)
            .setType("text/plain").setText(shareText).getIntent();
        actionProvider.setShareIntent(shareIntent);
        return true;
    }
    

    另一个选项,您可以通过将包名称添加到您的意图来直接指定应用程序以打开您的意图。

    sendIntent.setPackage("com.whatsapp");
    startActivity(sendIntent);
    

    有关更多信息,请查看文档whatsappandroid

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多