2013-05-21 204 views
-1

我正在嘗試發短信發送應用,但短信沒有發送。請指導。短信不發送

package com.example.smsbomber; 


import android.R.string; 
import android.app.Activity; 

import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.view.Menu; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     sendLongSMS(); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    public void sendLongSMS() { 
     String phone ="03322044685"; 
     SmsManager smsManager =  SmsManager.getDefault(); 
     smsManager.sendTextMessage(phone, null, "Message", null, null); 
    } 
} 
+1

你有'<使用許可權的android:NAME =「android.permission.SEND_SMS」 />'在'AndroidManifest.xml'和你想從一個仿真器或一個真正的手機發送? – Darwind

回答

0

這是我使用的代碼,它適用於我,你可以試試看。 當然,不要忘記清單文件中需要的權限。

String number = task.getMobile().trim(); 
    String sms = "Your sms massage"; 
      try 
      { 
      SmsManager smsManager = SmsManager.getDefault(); 
      smsManager.sendTextMessage(number, null, sms, null, null); 
      Toast.makeText(getApplicationContext(), "SMS: " + sms + " Sent!", Toast.LENGTH_LONG).show(); 
      } 
      catch (Exception e) 
      { 
      Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
      }