AerRayes
AerRayes

Reputation: 69

Android SMS intent stuck with the same body and same recepient

So my android application detects when location changes and then it notifies the user and makes him take action either call a number or send SMS.

The SMS is sent to a saved number and its body is "I'm at " + fullAddress

private NotificationCompat.Builder buildNormal(CharSequence pTitle,String fullAddress) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this);

    Intent in = new Intent(this,MainActivity.class);
    PendingIntent pMainIntent = PendingIntent.getActivity(this, 0,
            in, 0);

    if(getSavedDataString("gNumber")!=null) 
    {
        String url = getSavedDataString("gNumber");
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        //Intent smsIntent = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:" 
        //+ Uri.encode(getSavedDataString("gNumber").substring(4))));
        //intent.setData());
        //startActivity(intent);
        Intent smsIntent = new Intent(Intent.ACTION_VIEW);
        //smsIntent.setType("vnd.android-dir/mms-sms");
        smsIntent.putExtra("address", getSavedDataString("gNumber").substring(4));
        smsIntent.putExtra("sms_body","I'm at " + fullAddress);
        smsIntent.setData(Uri.parse("smsto:" 
        + Uri.encode(getSavedDataString("gNumber").substring(4))));
        PendingIntent psmsIntent = PendingIntent.getActivity(this, 0,
                smsIntent, 0);

        builder.addAction(android.R.drawable.ic_menu_call, "Call", pIntent);
        builder.addAction(android.R.drawable.sym_action_email, "Send SMS", psmsIntent);
    }
    else
    {
        builder.addAction(0, "Choose Guardian", pMainIntent);
    }
    builder.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);
    // set the shown date
    builder.setWhen(System.currentTimeMillis());
    // the title of the notification
    builder.setContentTitle(pTitle);
    // set the text for pre API 16 devices
    builder.setContentText(pTitle);
    // set the action for clicking the notification

    builder.setContentIntent(pMainIntent);
    // set the notifications icon
    builder.setSmallIcon(R.drawable.ic_home);
    //builder.setSound(android.)
    // set the small ticker text which runs in the tray for a few seconds
    builder.setTicker("Location Change Alert");
    // set the priority for API 16 devices
    //builder.setVibrate(pattern)
    builder.setPriority(Notification.PRIORITY_DEFAULT);
    return builder;
}

it shows here that the notification that shows to the user contains 2 actions to call or to send message and it sends it to the presaved number gNumber

The problem is after i press the action to send sms and then discard that message without sending it also deleting it from draft and all that. and then the app detects another location change so it sends a different notification with different fullAddress the intent is still stuck at the same text body!!

I also tried to change the recipient and it gets stuck on the old recipient too. I have to either restart the device or send the message I once discarded.

I also tried to change from ACTION_VIEW to ACTION_SEND or ACTION_SENDTO but all in vain.

I want to know if there's a solution to this intent getting stuck on the same body and recipient other than changing this intent totally and using SMSManager

Help please.

Upvotes: 0

Views: 260

Answers (1)

Mike M.
Mike M.

Reputation: 39181

When your app requests a PendingIntent, the system keeps a token on behalf of your app to perform an action as though your app is actually doing it. It is done this way so that, even if your app is killed, whatever process receives the PendingIntent can still follow through with it.

When these tokens are created, certain info is recorded, e.g. the operation, the the action, etc. If your app were to request another PendingIntent with the same info, the same token will be returned. Now, the info used to determine if they are the same or different does not include the extras that the Intent itself carries. So when your app requests the same SMS operation with only different Intent extras, you're going to get the same token with the original extras over and over again, unless you pass a flag to indicate differently.

As for the difference between the flags: FLAG_CANCEL_CURRENT "ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT." In your case, I don't think this is an issue, so FLAG_UPDATE_CURRENT should be sufficient. If it is a concern, use FLAG_CANCEL_CURRENT.

The quoted is directly from the docs here.

Upvotes: 2

Related Questions