从通知中打开默认地图应用程序

时间:2016-09-26 09:02:44

标签: android notifications broadcastreceiver

在我的代码中,我有一个接收传入短信的广播接收器。 如果邮件来自特定号码,并以单词"位置"开头,我想显示通知。当用户点击通知时,我需要打开Goog​​le地图,指明其中包含的位置消息。 但即使没有点击通知,Google地图也会打开。 这是我的代码

                if (message.startsWith("Location")) {
                    // abort
                    abortBroadcast();
                    String[] locationMessage = message.split(":");
                    String latitude = locationMessage[2];
                    String longitude = locationMessage[3];
                    String trueLocation = locationMessage[8];
                    float lat = Float.parseFloat(latitude);
                    float lon = Float.parseFloat(longitude);

                    Uri gmmIntentUri = Uri.parse("geo:" + lat + "," + lon + "?q=" + lat + "," + lon); // URI encoding
                    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                    mapIntent.setPackage("com.google.android.apps.maps");  // Check if there is map app installed
                    mapIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    if (mapIntent.resolveActivity(context.getPackageManager()) != null) {
                        //context.startActivity(mapIntent); // If map app is there starts map app
                        if (trueLocation.equals("false")) {
                            notification(context, "Find Pal", "GPS is turned off from Other side: Location", latitude, longitude, mapIntent);
                        } else {
                            notification(context, "Find Pal", "Location", latitude, longitude, mapIntent);
                        }
                    }

                }

private void notification(Context context, String s, String s1, String latitude, String longitude, Intent mapIntent) {
    NotificationCompat.Builder mBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.notification_icon_two)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.findpal_actionbar_icon))
                    .setContentTitle(s)
                    .setWhen(System.currentTimeMillis())
                    .setContentText(s1 + ":" + latitude + "," + longitude)
                    .setAutoCancel(true);
    PendingIntent intent2 = PendingIntent.getBroadcast(context, 1, mapIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(intent2);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());
}

1 个答案:

答案 0 :(得分:0)

也许你已经解决了问题,但无论如何都要尝试改变

PendingIntent.getBroadcast

PendingIntent.getActivity