【问题标题】:How to Send an SMS to Myself如何给自己发短信
【发布时间】:2012-09-12 08:40:45
【问题描述】:

如何在 Android 上向自己发送短信?我想输入发件人和虚构内容,并让它出现在短信通知栏中。

【问题讨论】:

    标签: java android sms


    【解决方案1】:
    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);
    

    【讨论】:

    • 不是我想要的。我 phoneNo = 我的设备?给自己留言?
    • 示例 ContentValues 值 = new ContentValues(); values.put("地址", "123456789"); values.put("body", "foo bar"); getContentResolver().insert(Uri.parse("content://sms/sent"), values);意图 sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms://"));开始活动(发送意向);只是不想立即阅读消息想留在通知栏中。
    【解决方案2】:

    如果你想给自己发消息,那么你可以使用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);
    }
    

    【讨论】:

    • 表示您需要在收件箱中发送此通知?
    • 您无法在收件箱中存储通知。通知仅用于通知。您可以使用通知管理器通知自己第二次或使用短信管理器并将此短信存储到收件箱中。
    • 如何使用短信管理器并将这条短信存储到收件箱中?帮助。
    • 谢谢所有的答案,可以。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多