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();
Related
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/
I'm trying to send a text message and my code ain't working. Can anyone help me out with that?
I tried using code given in tutorials point but its not working.
protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo,null,message,null,null);
Toast.makeText(getApplicationContext(), "SMS sent.",Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"SMS faild.",Toast.LENGTH_LONG).show(); e.printStackTrace(); }
}
Try like this:
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
Also check this Sending a SMS Message from an Android Application
It's SMSManager.sendTextMessage. Remember to request permission in your manifest.
So I am trying to make my app send an SMS automatically to the given number when the user presses the button.
I can make it open the messenger and write the text but I can't make it send it automatically.
My code is as follows (The part that matters I guess);
#Override
public void onClick(View a) {
if(a.equals(sms)){
tekst = (TextView) findViewById(R.id.txt);
Uri tlf = Uri.parse("smsto:"+tekst.getText().toString());
Intent c = new Intent(Intent.ACTION_VIEW, tlf);
c.setData(tlf);
c.putExtra("sms_body","Hjelp jeg er i fare!" );
startActivity(c);
}else{
tekst = (TextView) findViewById(R.id.txt);
Intent c = new Intent(Intent.ACTION_CALL);
Uri tlf = Uri.parse("tel:"+tekst.getText().toString());
c.setData(tlf);
startActivity(c);
}
}
So, how can I make it send the SMS?
BTW, I have added the permission: "android.permission.SEND_SMS"
Try with this:
String phoneNumber = "<phone_number_here>";
String message = "Test Message";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Notice that it's a very simple snip code that you can implement more.
If you want to see SMS showing up in any of the other SMS Clients/Apps installed yuo must use:
ContentValues values = new ContentValues();
values.put("address", "<phone_number_here>");
values.put("body", "Test Message");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
and add:
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
try this
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage([number], null, [sms], null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
where [number] is the number to which you want to send your sms and [sms] is your text you wanna send
Try this one.
//Declare the button and the tetviews to input number and the message
Button sendSMSBtnBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
txtMessage = (EditText) findViewById(R.id.editTextSMS);
//Calling the method sendSMSMessage in the button click event
sendSMSBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});}
// Method to send SMS using SMS Manager
protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Note : Make sure you have the set the following permission in the Manifest file.
<uses-permission android:name="android.permission.SEND_SMS" />
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.
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!