通过意图通知打开谷歌地图

时间:2016-07-20 15:38:41

标签: java android google-maps android-notifications

我有一个侦听传入短信的BroadcastReciver。当短信来时,它会显示通知。我想,当用户打开通知时,它会转到结果活动,并且它会通过意图打开谷歌地图。 我想我写的一切都好,但它没有用。单击通知后,将打开一个空白页。

我的incomingSms BroadcastReciver:

public void ShowNotif(String from , String body , Context con){

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(con)
                    .setSmallIcon(R.drawable.ic_search)
                    .setContentTitle(from)
                    .setContentText(body);
    int NOTIFICATION_ID = 12345;

    Intent targetIntent = new Intent(con,Resualt.class );
    targetIntent.putExtra("loc",body);
    PendingIntent contentIntent = PendingIntent.getActivity(con, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);
    NotificationManager nManager = (NotificationManager) con.getSystemService(Context.NOTIFICATION_SERVICE);
    nManager.notify(NOTIFICATION_ID, builder.build());
    Intent goResIntent = new Intent(con , Resualt.class);
    con.startActivity(goResIntent);


    // gogole map intent :


}

和Resualt acitivty:

public class Resualt extends Activity {
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.resualt);
     double Mylatitude = 12;
     double Mylongitude = 11;
    GPSTracker tracker = new GPSTracker(this);
    if (tracker.canGetLocation()) {
        Mylatitude = tracker.getLatitude();
        Mylongitude = tracker.getLongitude();
    }
    Intent intent = getIntent();
    String location = intent.getStringExtra("loc");
    String ACC_lat = location.substring(0, location.indexOf(","));
    String ACC_lang = location.substring(location.indexOf(",") + 1, location.length());
    Toast.makeText(this, 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));
    startActivity(mapIntent);
  // this.finish();
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您可以通过通知服务中的待定意图来打开单个空白活动,然后在该空白活动上打开地图意图并完成该空白活动。 从通知数据接收的链接可以通过意向作为额外发送

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("location_link", link);