2016-09-26 39 views
0

在我的代码中,我有一个接收传入SMS的广播接收器。 如果消息来自特定的号码,并以“位置”字开头,我想显示通知。当用户单击通知时,我需要打开Goog​​le Map,指出消息中包含的位置。 但即使没有点击通知,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()); 
} 

回答

0

也许您解决您的问题,但无论如何,尝试改变

PendingIntent.getBroadcast 

PendingIntent.getActivity 
相关问题