1

我正在开发一个发送短信并接收一些响应的应用程序。

现在的问题是 SMS 发送在 2.2 froyo 中工作正常,但在 ICS 4.0 中无法正常工作。

我在我的代码中完成了以下操作:

  1. 在我的 Manifest tp 中添加了接收和发送 SMS 的权限。

    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    
  2. 这是我在按钮中用来发送短信的代码。

    public void sendSms(String phoneNumber,String sms) {      
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNumber, null ,sms,  null, null);            
    }
    
4

1 回答 1

1

@rciovati 感谢您的建议,我使用 SMS 方法添加了两个待处理的意图,现在一切正常这是代码

SmsManager smsManager=SmsManager.getDefault();
 String SENT = "SMS_SENT";
 String DELIVERED = "SMS_DELIVERED";     
 public void sendSms(String phoneNumber,String sms){
     PendingIntent pi = PendingIntent.getActivity(this, 0,
                new Intent(this, myService.class), 0);   
     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 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));

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


        smsManager.sendTextMessage(phoneNumber, null, sms, sentPI, deliveredPI);

    }
于 2013-05-02T15:36:08.117 回答