共享位置信息使用共享意图活动

13

我需要使用共享意图活动从我的应用程序共享位置,我已经查看了一些示例并知道如何实现共享意图。但是我在setType方面遇到了困难。我需要我的应用程序共享位置详细信息以及地图上的用户位置。

顺便说一句,我复制了一个用户的代码,这个问题非常类似,希望不会有任何冒犯。

真的非常感谢任何帮助。

Intent intent1 = new Intent(Intent.ACTION_SEND); 
intent1.setType("text/plain"); 
intent1.putExtra(Intent.EXTRA_TEXT, "The status update text"); 
startActivity(Intent.createChooser(intent1, "Select prefered Service")); 
4个回答

15

以下是触发地图定位意图的代码:

String uri = "geo:" + latitude + ","
                    +longitude + "?q=" + latitude
                    + "," + longitude;
startActivity(new Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse(uri)));

3
非常感谢,但这不是我想要的。你的代码打开了谷歌地图。我不想启动谷歌地图,我只需要我的应用程序能够分享我的当前位置,就像谷歌地图分享位置一样。 - Princewill Obinna Iwuorie
1
只是为了提醒任何使用此示例并运行的人添加一条注释。如果您自定义“q”参数,请确保安全地构建Uri:String uri = Uri.Builder().scheme("geo").appendPath(lat +","+ lng).appendQueryParameter("q", name).build(); - Joel Rosenberg
你需要使用Intent.createChooser来打开一个选择对话框,其中包含支持该意图的所有应用程序。请参见其他答案。 - IgniteCoders

10
Double latitude = user_loc.getLatitude();
Double longitude = user_loc.getLongitude();

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

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String ShareSub = "Here is my location";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, ShareSub);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, uri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));

3
    String uri = "https://www.google.com/maps/?q=" + latitude+ "," +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..."));

这个答案应该被接受。 - Husniddin Muhammad Amin

1

针对Kotlin用户

val uri = "https://www.google.com/maps/?q=$latitude,$longitude"
        val sharingIntent = Intent(Intent.ACTION_SEND)
        sharingIntent.type = "text/plain"
        sharingIntent.putExtra(Intent.EXTRA_TEXT, uri)
        startActivity(Intent.createChooser(sharingIntent, "Share in..."))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接