0

如何在 Android 上向自己发送短信?我想输入发件人和虚构内容,并将其显示在 SMS 通知栏中。

4

3 回答 3

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

同样在您的 Android Manifest 文件中,添加以下权限:

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

或者,如果您想发送超过 160 个字符的短信:

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);
于 2012-09-20T01:12:37.123 回答
1

如果您想向自己发送消息,则可以使用通知管理器
当你想得到通知时调用下面的函数。为此通知传递消息字符串和标题字符串。 这是给你的好教程

您还需要Pending Intent 在特定任务完成后或同时发送此通知。Pending Intent允许外部应用程序使用您的应用程序的权限来执行一段预定义的代码。
这是可能对您有所帮助的代码。

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);
}
于 2012-09-20T11:23:32.887 回答
0

如果 SMS 不是强制性的,您也可以向自己发送聊天消息。

实现这一目标的两个项目:

于 2017-10-13T14:49:09.030 回答