SoFunction
Updated on 2025-04-07

Example of Android programming to implement timing text message function

This article describes the implementation of the timed text message function of Android programming. Share it for your reference, as follows:

First, to realize the function of sending text messages, you must use the permissions of sending text messages in the Android system, that is, add the following content to it.

<uses-permissionandroid:name=".SEND_SMS"/>

Second, use AlarmManager to implement a countdown function, sending short messages when the time comes. AlarmManager has two similar uses:

1. Perform an operation at a specified time.
2. Perform an operation periodically. The AlarmManager object needs to be used with the Intent object. You can start an activity regularly, send a Broadcast, or enable a Service.

Here is the core code snippet:

AlarmManager aManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(this,);
("AlarmReceiver");
PendingIntent pendingIntent=(this, 0, intent, 0);
//(, (), pendingIntent);
(, 0, 60*1000, pendingIntent)

Third, realize the time setting method.

1. You can directly use the set method of the AlarmManager object to set the specific alarm time.
2. You can use TImePicker to set the time, which is more flexible.

Fourth, create a new AlarmReceiver class to respond to the alarm clock.

1. Now add Receiver's statement

<receiver
android:name=".AlarmReceiver"
  android:label="@string/app_name">
  <intent-filter>
    <action android:name="AlarmReceiver" />
  </intent-filter>
</receiver>

It realizes the functions of obtaining time and sending text messages. Send SMS messages requires the SmsManager class, and use the class SmsManager to send information. SmsManager is a default instance of SmsManager.

SmsManager smsManager =();

Its method is as follows

(destinationAddress,scAddress, text, sentIntent, deliveryIntent)

The meaning of each parameter is as follows

destinationAddress: Recipient number
scAddress: SMS center service number, set to null here
text: Send content
sentIntent: Send the SMS result status signal (whether it was successfully sent), new an Intent, and the operating system will broadcast the Intent after receiving the signal. This process is asynchronous.
deliveryIntent: The other party receives the status signal (whether it has been successfully received).

Finally, the detailed code is attached

public class YoulainaozhongActivity extends Activity {
  TextView onetextview;
  TextView twotextview;
  TextView threetextview;
  Button onebutton;
  Button twobutton;
  Dialog dialog = null;
  // Create a new calendar object to set the alarm time  Calendar calendar = ();
  private SharedPreferences sharedPreferences;
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    (());
    LinearLayout relativeLayout =(LinearLayout) findViewById();
      ();
    onebutton=(Button) findViewById();
    (new OnClickListener() {
      @Override
      public void onClick(View v) {
        dialog();
      }
    });
    twobutton=(Button) findViewById();
    (new OnClickListener() {
      @Override
      public void onClick(View v) {
        finish();
      }
    });
    onetextview=(TextView) findViewById();
    twotextview=(TextView) findViewById();
    threetextview=(TextView) findViewById();
    sharedPreferences=getSharedPreferences("alarm_record", Activity.MODE_PRIVATE);
    AlarmManager aManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent intent=new Intent(this,);
    ("AlarmReceiver");
    PendingIntent pendingIntent=(this, 0, intent, 0);
//   (, (), pendingIntent);
    (, 0, 60*1000, pendingIntent);
  }
  public void dialog(){
    View view=getLayoutInflater().inflate(, null);//
    final TimePicker timePicker=(TimePicker)();
    final EditText oneeditext=(EditText)();
    final EditText twoeditext=(EditText)();
    timePicker.setIs24HourView(true);
    new (this)
    .setTitle("set up")
    .setView(view)
    .setPositiveButton("Sure", new () {
    public void onClick(DialogInterface dialog, int which) {
    String timeStr=(())+":"+(());
    /*(Calendar.HOUR_OF_DAY,());
    (, 55);*/
    ("You set the time: "+timeStr);
    ("The number you set is: "+().toString());
    ("The content you set is: "+().toString());
    ().putString(timeStr, timeStr).commit();
    ().putString("haoma", ().toString()).commit();
    ().putString("neirong", ().toString()).commit();
    }
    }).setNegativeButton("Cancel", null).show();
  }
}
public class AlarmReceiver extends BroadcastReceiver {
  /**
    * Scan through the broadcast to see if the alarm rings again after the time is reached
    * */
  @Override
  public void onReceive(Context context, Intent intent) {
    SharedPreferences sharedPreferences = (
        "alarm_record", Activity.MODE_PRIVATE);
    String hour = (().get(
        Calendar.HOUR_OF_DAY));
    String minute = (().get(
        ));
    String time = (hour + ":" + minute, null);// Hours and minutes,    String haoma = ("haoma", null);
    String neirong = ("neirong", null);
    if (time != null) {// Determine whether it is empty, and then create it,//     MediaPlayer mediaPlayer = (context, );
      (context, "SMS has been sent successfully", Toast.LENGTH_LONG).show();
//     ();// start ;      sendMsg(haoma, neirong);
    }
  }
  private void sendMsg(String number, String message) {
    SmsManager smsManager = ();
    (number, null, message, null, null);
  }
}

For more information about Android related content, please check out the topic of this site:A summary of Android SMS and Telephone Operation Tips》、《Android file operation skills summary》、《Summary of Android operating json format data skills》、《Android programming activity operation skills summary》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.