2013-10-01 75 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); 

,希望能给予你,为什么这是失败的更多信息。