2012-07-16 54 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);   
    }