Android broadcase new text message

To broadcast a new text message on Android, you can use the android.intent.action.NEW_OUTGOING_CALL intent. This intent is sent when a new outgoing call or SMS is initiated.

Here's an example of how you can use this intent in your Android app:

  1. Create a new intent:
    Intent intent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
  2. Set the data of the intent to the phone number and message:
    intent.setData(Uri.parse("sms:" + phoneNumber));
    intent.putExtra("sms_body", message);
  3. Send the intent:
    sendBroadcast(intent);

    Here's the complete code:

    Intent intent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
    intent.setData(Uri.parse("sms:" + phoneNumber));
    intent.putExtra("sms_body", message);
    sendBroadcast(intent);

    Note that this intent is only sent when the user initiates a new outgoing call or SMS. If you want to send a broadcast message programmatically, you can use the android.intent.action.SENDTO intent instead.

Here's an example of how to use the android.intent.action.SENDTO intent:

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + phoneNumber));
intent.putExtra("sms_body", message);
sendBroadcast(intent);

This intent is sent when the user initiates a new SMS message, and the sms_body extra contains the message to be sent.

You can also use the android.intent.action.SEND intent to send a broadcast message, but this intent is used for sending messages to other apps, not for sending SMS messages.

Here's an example of how to use the android.intent.action.SEND intent:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, message);
sendBroadcast(intent);

This intent is sent when the user initiates a new message to another app, and the EXTRA_TEXT extra contains the message to be sent.

Note that the sendBroadcast method sends the intent to all registered receivers, including the default SMS app. If you want to send the intent to a specific app, you can use the startActivity method instead:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(intent);

This method starts the specified app and passes the intent to it.