Unable to send sms programatically in android - android

Hi i am a new android developer.i am trying to send sms through android built-in service
SmsManager class my code is running accurately but the message sent through this is not received to other number.My code is as follows
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String ph=et1.getText().toString();
String text=et2.getText().toString();
try{
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(ph,null, text,null,null);
Toast.makeText(getApplicationContext(), "sent", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Message not sent", Toast.LENGTH_SHORT).show();
}
}
});

Please check following permission
android.permission.SEND_SMS

This works perfect in my application
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sendSMS("YOURNUMBER", Integer.toString(scaleFactor));
Toast toast = Toast.makeText(getApplicationContext(),"Sending msg...", Toast.LENGTH_SHORT);
toast.show();
}
});
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
and in Manifest :
<uses-permission android:name="android.permission.SEND_SMS" />

Do you have a permission? android.permission.SEND_SMS ? You must add it if you want to send messages.
Try this code: http://blogs.wrox.com/article/sending-sms-messages-programmatically-in-your-android-application/

Related

SmsManger.getDefault(); cannot be resolved error

There is an error in my code like SmsManger.getDefault(); cannot be resolved error
Button b2 = (Button)findViewById(R.id.btn1);
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Send Sms", null);
String phone="0899786592";
String message="Rescue Me: /n I am at Lattitude- "+lat+" ,Long- "+lon;
SmsManager smsManager =new SmsManager.getDefault()
//getting error in the above line i also tried without type casting.. works but the smsManager is getting an error
try{
smsManager.sendTextMessage(phone,null,message, null, null);
Toast.makeText(getApplicationContext(),"Sms Sent", Toast.LENGTH_SHORT).show();
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Sms not sent!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
}
You cannot instantiate SmsManager, it is a Singleton and has a private constructor. That's why you are getting an error with new SmsManager.getDefault().
To get an instance, you just need to call the static method : getDefault.
SmsManager smsManager = SmsManager.getDefault();

Sending Encoded SMS using SmsManager does not get received

When in send direct SMS there is no problem but when I send operational SMS that contains DNA bases(A , G , T , C only) then it is not working.
Plaintext is normal message. Whats the problem?? Please help.
public class sendMessage extends Activity {
Button button;
EditText plainTxt;
EditText cipherText;
EditText editPhoneNum;
int plaintxtArray[] = new int[1500];
Bundle bundle=new Bundle();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.smssend);
button = (Button) findViewById(R.id.button);
editPhoneNum = (EditText)findViewById(R.id.editPhoneNum);
plainTxt = (EditText) findViewById(R.id.editSMS);
cipherText = (EditText)findViewById(R.id.editcipher);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String phoneNo = editPhoneNum.getText().toString();
//Toast.makeText(getBaseContext(), "Number is: " + phoneNo, Toast.LENGTH_LONG).show();
String plainText = plainTxt.getText().toString();
String CipherText=DNAbaseConvert(plainText);
Toast.makeText(getBaseContext(), "Cypher Text is: " + CipherText, Toast.LENGTH_LONG).show();
MessageToSent( phoneNo, CipherText);
}
});
}
public String DNAbaseConvert(String plainText)
{
//various operation goes here.
return b; //b-> a string , length 7-8 charecter long.
}
public void MessageToSent(String phoneNo, String CipherText) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, CipherText, 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();
}
}
public void onBackPressed() {
super.onBackPressed();
Intent www = new Intent(sendMessage.this, LoggedIn1.class);
startActivity(www);
finish();
}
}
You can try this:
try {
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(CipherText);
smsManager.sendMultipartTextMessage(phoneNo, null, parts, 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();
}
For more help you can see this thread
Try saving the response from DNAbaseConvert(plainText) in a variable and pass it to sendTextMessage()
String msg=DNAbaseConvert(plainText);
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
This is because the response from DNAbaseConvert() may caused problem inside it..
You might be having a problem hitting the SMS message size limit. If you're using the SmsManager.sendTextMessage() method, you might instead try the SmsManager.sendMultipartTextMessage() method, with the SmsManager.divideMessage() method to split up your string.

Error while sending SMS

I'm writing code for sending SMS, but sending the SMS is failing. Additionally, sometimes my code has a crossed line and this warning: "The method sendTextMessage(String, String, String, PendingIntent, PendingIntent) from the type SmsManager is deprecated"
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();
}
}
}
You're getting a deprecation message because you've imported the wrong SmsManager Class.
Remove android.telephony.gsm.SmsManager and import android.telephony.SmsManager
Also, please make sure you've given the permission to send messages
<uses-permission android:name="android.permission.SEND_SMS" />

Send SMS failed via Android Device

I am writing an app in which i am trying to send SMS to a Recepient, but whenever i do click on Send, getting message:- SMS faild, please try again later!
Either i am using Emulator or Android Device....
Like you can see in below screen shot, here i am trying to send message to Pratik, which is saved in my Phonebook Contacts, but whenever i am trying to message to Pratik not able to send message to Pratik
Please see below screen shot, like you can see, here i am trying to send message to Rahul...
Manifest.xml:
<uses-permission android:name="android.permission.SEND_SMS" />
Please check below code:
private TextView name;
private ListView list;
private Database db;
private Contact contact;
ImageButton buttonSend;
EditText textSMS;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
// bind GUI components
this.name = (TextView) findViewById(R.id.editor_name);
this.list = (ListView) findViewById(R.id.editor_list);
// check if contact id is valid
this.db = new Database(getContentResolver());
int contactId = getIntent().getIntExtra(CONTACT_ID, NO_CONTACT_ID);
this.contact = this.db.getContact(contactId);
if (this.contact == null) {
finish();
}
this.name.setText(this.contact.getName());
// pre-load information about all account types
AuthenticatorDescription[] authTypes = AccountManager.get(this).getAuthenticatorTypes();
for (AuthenticatorDescription authDesc : authTypes) {
this.map.put(authDesc.type, authDesc);
}
// bind list events
this.list.setOnItemClickListener(this);
this.list.setOnCreateContextMenuListener(this);
// create the GUI
updateView();
saveGreeting = (ImageButton) findViewById(R.id.greeting);
saveGreeting.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
customGreeting(v);
}
});
buttonSend = (ImageButton) findViewById(R.id.buttonSend);
textSMS = (EditText) findViewById(R.id.editTextSMS);
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String sms = textSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(CONTACT_ID, null, sms, 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();
}
}
});
}
Do not use recipient name to send message , use mobile number to send message .
Get contact number from contact list and use that contact number instead of name in sendTextMessage function.

Save SMS to inbox when sending text using Sms Manager Android

I had a bit of code that i used to send an sms message to a number that the user entered by pressing a button. However, when the message sends, it doesn't show up in their messaging inbox so they don't know if it exactly sent or not. Is there any way i could alter the text below to save all the sms messages to the users inbox? Thanks!
This is my code:
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String phoneNo = textPhoneNo.getText().toString();
String sms = textSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "Message Sent!", Toast.LENGTH_LONG).show();}
catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Unable to send message",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
Add this method to your class
private void addMessageToSent(String telNumber, String messageBody) {
ContentValues sentSms = new ContentValues();
sentSms.put("address", telNumber);
sentSms.put("body", messageBody);
ContentResolver contentResolver = getContentResolver();
contentResolver.insert(Uri.parse("content://sms/sent"), sentSms);
}
Hope it helps!

Categories

Resources