【发布时间】:2012-09-12 08:40:45
【问题描述】:
如何在 Android 上向自己发送短信?我想输入发件人和虚构内容,并让它出现在短信通知栏中。
【问题讨论】:
如何在 Android 上向自己发送短信?我想输入发件人和虚构内容,并让它出现在短信通知栏中。
【问题讨论】:
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);
【讨论】:
如果你想给自己发消息,那么你可以使用notification Manager。
当你想得到通知时调用下面的函数。为此通知传递消息字符串和标题字符串。 Here is good tutorial for you
您还需要 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);
}
【讨论】:
如果短信不是强制性的,您也可以向自己发送聊天消息。
实现这一目标的两个项目:
【讨论】: