2016-09-26 127 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