简体   繁体   中英

How to Send an SMS to Myself

How can I send an SMS to myself on Android? I want to enter a sender and fictitious content, and have it appear in the SMS notifications bar.

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("DESTINATION NUMBER", null, "Your Message Text", null, null);

Also in your Android Manifest file, add the following permission:

uses-permission android:name="android.permission.SEND_SMS"

Or if you would like to send a SMS with more than 160 characters:

String phoneNo = "INSERT NR HERE";
String message = "This is a long message that is supposed to be longer than 160 characters.";

SmsManager smsManager = SmsManager.getDefault();
ArrayList msgparts = smsManager.divideMessage(message);

smsManager.sendMultipartTextMessage(phoneNo, null, msgparts, null, null);

If you want to send message to yourself, then you can use notification Manager .
Call below function when you want get notification. Pass message string and title string for this notification. Here is good tutorial for you

You also need Pending Intent to send this notification after specific task completed or while. Pending Intent allows the foreign application to use your application's permissions to execute a predefined piece of code.
Here is code which might be help you.

protected void sendnotification (String title, String message) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);       
int icon = R.drawable.icon;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = title;
CharSequence contentText = message;
Intent notificationIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);
}

If SMS is not mandatory, you can also send chat messages to yourself.

Two projects achieving this :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM