【问题标题】:sending sms by intent有意发送短信
【发布时间】:2012-07-16 02:41:31
【问题描述】:

我已经阅读了之前发布的所有文档和问题,但我找不到自己的答案。通过编写以下代码,我想在附近时发送短信。

首先,我想检查一下意图是否只创建短信而不发送?如果没有,有没有办法可以发送短信以获取接近警报。 其次,proximityalert由于某些原因没有启动,是代码有问题吗?

谢谢。

Intent smsintent = new Intent(android.content.Intent.ACTION_VIEW);

smsintent.putExtra("address", "96424127"); 
smsintent.putExtra("sms_body", "child alert");
smsintent.setType("vnd.android-dir/mms-sms"); 

PendingIntent pendintent = PendingIntent.getService(this, 0, smsintent, 0);
lm.addProximityAlert(103.8497215, 1.3630663, 1000, 100000000, pendintent);`

【问题讨论】:

标签: android android-intent


【解决方案1】:

试试这个 smsintent.setData(Uri.parse("sms:"));

【讨论】:

  • 它仍然只激活短信应用程序,创建短信但它不会自动发送。有没有办法在创建短信后自动发送它
【解决方案2】:

您应该使用registerReceiver 方法和PendingIntent 类来发送短信。下面我发布我的代码以发送短信。:-

  private void sendSMS(String phoneNumber, String message)
    {        
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        

        android.telephony.SmsManager sms = android.telephony.SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
    }

【讨论】:

    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 2019-04-24
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多