【问题标题】:Intent to google map not working谷歌地图的意图不起作用
【发布时间】:2016-07-21 07:47:28
【问题描述】:

我的应用中有一个 BroadCastReciver,用于侦听传入的短信。 我想在收到短信时,应用程序通过意图打开谷歌地图。这是我的代码,但我不知道我的错误在哪里。 谢谢你的帮助。

BroadcastReciver.class:

public class IncomingSms extends BroadcastReceiver {

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(Context context, Intent intent) {

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();
                    startAct(message, context);
                } // end for loop
            } // bundle is null

        } catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" + e);

        }
    }
    private void startAct(String message, Context con) {

        double Mylatitude = 12;
        double Mylongitude = 11;
        GPSTracker tracker = new GPSTracker(con);
        if (tracker.canGetLocation()) {
            Mylatitude = tracker.getLatitude();
            Mylongitude = tracker.getLongitude();
        }

        String location = message;
        String ACC_lat = location.substring(0, location.indexOf(","));
        String ACC_lang = location.substring(location.indexOf(",") + 1, location.length());
        Toast.makeText(con, ACC_lang + " ^ " + ACC_lat, Toast.LENGTH_LONG).show();
        Intent mapIntent = new Intent(android.content.Intent.ACTION_VIEW,
                Uri.parse("http://maps.google.com/maps?saddr=" + Mylatitude + "," + Mylongitude + "&daddr=" + ACC_lat + "," + ACC_lang));
    con.startActivity(mapIntent);

   }

我也在manifest.xml文件中添加了它

【问题讨论】:

  • 那么您检查是否已更正纬度、经度、ACC_lat 和 ACC_lang?
  • 尽可能打印您的日志。你在哪里遇到问题?你用的是androidM吗?

标签: android google-maps location broadcastreceiver


【解决方案1】:

你需要将 URI 分成两部分,一个在 Intent 的 costructor 中使用,另一个作为包。这里是more details,如何发送意图。

示例:

Uri gmmIntentUri = Uri.parse("geo:37.7749,-122.4194");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(mapIntent);
}

【讨论】:

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