短信经理不工作

时间:2016-08-16 17:35:19

标签: android sms smsmanager

我正在使用短信管理器发送短信,其中包含已发送的待处理意图和已发送的待处理意图。我的应用程序仅使用短信发送功能,我也已经放置了此权限

<uses-permission android:name="android.permission.SEND_SMS"/>

以下是片段OnAcitivityCreated方法中的代码:

try {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

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

    PendingIntent deliveredPI = PendingIntent.getBroadcast(getActivity().getApplicationContext(), 0,
            new Intent(DELIVERED), 0);
    //PendingIntent sentPI;
    //String SENT = "SMS_SENT";
   // sentPI = PendingIntent.getBroadcast(getActivity().getApplicationContext(), 0,new Intent(getActivity().getApplicationContext(),MainActivity.class), 0);

    getActivity().registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getActivity().getApplicationContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getActivity().getApplicationContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getActivity().getApplicationContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getActivity().getApplicationContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getActivity().getApplicationContext(), "Radio off",
                            Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    },new IntentFilter(SENT));

    SmsManager smsmanager = SmsManager.getDefault();
    smsmanager.sendTextMessage("+923349521400", null, message, sentPI, deliveredPI);
    Toast.makeText(getActivity(), "Sms sent!", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
    Toast.makeText(getActivity(), "failure", Toast.LENGTH_LONG).show();

}

1 个答案:

答案 0 :(得分:1)

希望这段代码可以帮助你:)

public class MainActivity extends Activity {
    private int mMessageSentParts;
    private int mMessageSentTotalParts;
    private int mMessageSentCount;
     String SENT = "SMS_SENT";
     String DELIVERED = "SMS_DELIVERED";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String phoneNumber = "0000000000";
                String message = "Hello World!";
                sendSMS(phoneNumber,message);


            }
        });



    }


    public void sendSMS(String phoneNumber,String message) {
        SmsManager smsManager = SmsManager.getDefault();


         String SENT = "SMS_SENT";
            String DELIVERED = "SMS_DELIVERED";

            SmsManager sms = SmsManager.getDefault();
            ArrayList<String> parts = sms.divideMessage(message);
            int messageCount = parts.size();

            Log.i("Message Count", "Message Count: " + messageCount);

            ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
            ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();

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

            for (int j = 0; j < messageCount; j++) {
                sentIntents.add(sentPI);
                deliveryIntents.add(deliveredPI);
            }

            // ---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, message, sentPI, deliveredPI);
           /* sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents); */
    }
}