2016-07-21 116 views
2

我有一個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文件

+0

所以,你檢查,你已經改正緯度,經度,ACC_lat和ACC_lang? –

+0

如果可能的話打印日誌。你在哪裏遇到問題?你使用的是androidM嗎? – Drv

回答

0

您需要URI分爲兩個部分,使用一個在意向書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); 
}