2012-07-16 141 views
0

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

首先,我想检查一下,如果意图只创建短信而不发送它?如果没有,是否有一种方法可以发送近距离警报的短信。 其次,由于某些原因,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);` 

+0

对此问题一看http://stackoverflow.com/questions/2372248/launch-sms-application-with-an-intent的http:// stackoverflow.com/questions/5013651/android-send-sms-intent-help http://stackoverflow.com/questions/4967448/send-sms-in-android如果你仍然有问题比去这两个教程http:///android-er.blogspot.in/2011/03/send-sms-using-intentactionsendto.html http://www.mkyong.com/android/how-to-send-sms-message-in-android/我希望通过这个链接之一,你会得到解决方案 – Aamirkhan 2012-07-16 04:38:39

回答

0

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

+0

它仍然只激活短信应用程序,创建短信,但它不会自动发送。是否有创建短信后自动发送的方法 – 2012-07-16 10:08:05

0

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

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