通过默认发件人在android中发送短信
我已经创建了一个简单的sms应用程序,其中会发送一条特定的消息,如“欢迎使用我们的应用程序”。我想从我的号码发送短信。所以我将发件人号码设置为我的号码,但它显示toast中的一般失败。有人能建议如何在android的sendtextmessage()函数中设置默认发件人号码吗。 我的代码 使用以下代码:通过默认发件人在android中发送短信,android,smsmanager,Android,Smsmanager,我已经创建了一个简单的sms应用程序,其中会发送一条特定的消息,如“欢迎使用我们的应用程序”。我想从我的号码发送短信。所以我将发件人号码设置为我的号码,但它显示toast中的一般失败。有人能建议如何在android的sendtextmessage()函数中设置默认发件人号码吗。 我的代码 使用以下代码: TelephonyManager tMgr=(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_服务); sphn=
TelephonyManager tMgr=(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_服务);
sphn=tMgr.getLine1Number()代码>
在AndroidManifest.xml中,授予以下权限:
我想设置一个默认号码,说开发者没有,短信包作为发送方。是否可以设置默认发送方号码?否您不能使用默认发送方号码。您可以为此使用一些批量短信服务提供商,或者您可以在应用程序中创建一些用于发送消息的api
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);
}
}