通过默认发件人在android中发送短信

时间:2017-05-24 10:03:50

标签: android smsmanager

我创建了一个简单的短信应用程序,其中特定的消息,如欢迎来到我们的应用程序'将被发送。我希望从我的号码发送短信。所以我设置发送者不是我的否,但它显示在吐司中的通用失败。有人可以建议如何在android中的sendtextmessage()函数中设置defaultsender编号。 我的代码

 package com.example.futuro.sms;


     public class MainActivity extends AppCompatActivity {
         private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 0;
           String phn, msg,sphn;

              @Override
          public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView t1 = (TextView) findViewById(R.id.f);
    t1.setText("hi");
    sphn="8111802225";
    phn = "9633512318";
    String message = "Thanks for downloading our app we will contact you soon. This is ur reference number";
    int randomPIN = (int) (Math.random() * 9000) + 1000;
    String pin = String.valueOf(randomPIN);
    msg = message + "PA" + pin;
    String ms="hi";
    sendSMS(phn,sphn, ms);
}

private void sendSMS(String phoneNumber, String sender,String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";TextView t=(TextView)findViewById(R.id.f);
    t.setText(sender);
    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:
                    ContentValues values = new ContentValues();

                        values.put("address",phn);// txtPhoneNo.getText().toString());
                        values.put("body", msg);

                    getContentResolver().insert(
                            Uri.parse("content://sms/sent"), values);
                    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 sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, sender, message, sentPI, deliveredPI);
}
    }

1 个答案:

答案 0 :(得分:0)

使用以下代码:
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE); sphn = tMgr.getLine1Number();

在AndroidManifest.xml中,授予以下权限:

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