通过意图发送短信

时间:2012-07-16 02:41:31

标签: android android-intent

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

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

感谢。

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);`

2 个答案:

答案 0 :(得分:0)

试试这个 smsintent.setData(Uri.parse( “SMS”));

答案 1 :(得分:0)

您应该使用registerReceiverPendingIntent类来发送短信。下面我发布我的代码发送短信。: -

  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);        
    }