2013-10-01 54 views
1

我正在編寫發送短信的代碼,但發送短信失敗。此外,有時我的代碼有交叉線和這樣的警告:「該方法sendTextMessage(字符串,字符串,字符串的PendingIntent,的PendingIntent)從類型SmsManager已過時」發送短信時出錯

class MainActivity extends Activity implements OnClickListener{ 

    Button bSend; 
    EditText Mobile, msg; 
    String mob, s_msg; 

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

     init(); 
     bSend.setOnClickListener(this); 
    } 

    private void init() { 
     // TODO Auto-generated method stub 
     bSend = (Button) findViewById(R.id.bSendSMS); 
     Mobile = (EditText)findViewById(R.id.etMobile); 
     mob = Mobile.getText().toString(); 
     msg = (EditText)findViewById(R.id.etMsg); 
     s_msg = msg.getText().toString(); 
    } 

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

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     try { 
      SmsManager smsManager = SmsManager.getDefault(); 
      smsManager.sendTextMessage(mob, null, s_msg, null, null); 
      Toast.makeText(getApplicationContext(), "SMS Sent!", 
        Toast.LENGTH_LONG).show(); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), 
        "SMS faild, please try again later!", 
        Toast.LENGTH_LONG).show(); 
      e.printStackTrace(); 
     } 
    } 

} 

回答

0

你得到一個過時消息,因爲您導入了錯誤的SmsManager類。 刪除android.telephony.gsm.SmsManager並導入android.telephony.SmsManager

另外,請確保你已經給了允許發送郵件

<uses-permission android:name="android.permission.SEND_SMS" /> 
0
smsManager.sendTextMessage(mob, null, s_msg, null, null); 

末兩個空,你可以把一個PendingIntent地方。第一個是用於接收短信,一旦發送短信,另一個用於收到短信。您可以添加以下代碼,以檢查由發送返回錯誤代碼:

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

    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 SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
        Toast.makeText(getBaseContext(), "Generic failure", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_NO_SERVICE: 
        Toast.makeText(getBaseContext(), "No service", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_NULL_PDU: 
        Toast.makeText(getBaseContext(), "Null PDU", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_RADIO_OFF: 
        Toast.makeText(getBaseContext(), "Radio off", 
          Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 
    }, new IntentFilter(SENT)); 

    smsManager.sendTextMessage(mob, null, s_msg, sentPI, null); 

,希望能給予你,爲什麼這是失敗的更多信息。