Android 发送短信时出错

Android 发送短信时出错,android,smsmanager,Android,Smsmanager,我正在编写发送短信的代码,但发送短信失败。此外,有时我的代码有一条交叉线,并且出现以下警告:不推荐使用SmsManager类型中的sendTextMessageString、String、String、PendingEvent、PendingEvent方法 class MainActivity extends Activity implements OnClickListener{ Button bSend; EditText Mobile, msg; String m

我正在编写发送短信的代码,但发送短信失败。此外,有时我的代码有一条交叉线,并且出现以下警告:不推荐使用SmsManager类型中的sendTextMessageString、String、String、PendingEvent、PendingEvent方法

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

}

您将收到一条弃用消息,因为您导入了错误的SmsManager类。 删除android.telephony.gsm.smsmsmanager并导入android.telephony.smsmsmanager

另外,请确保您已授予发送邮件的权限

<uses-permission android:name="android.permission.SEND_SMS" />
末端的两个零位是可以放置悬挂式帐篷的位置。第一个用于在发送文本消息后接收消息,另一个用于在接收时接收消息。您可以添加此代码以检查发送返回的错误代码:

   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);
这将有希望给你更多的信息,为什么这是失败的

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