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" />
Related
I want to send SMS after reject incoming call.
App is sending the SMS but the problem is that it is sending it twice. I am unable to figure out where problem is.
Below is the code used.
public class CallBarring extends BroadcastReceiver
{
private String number;
#Override
public void onReceive(Context context, Intent intent)
{
// If, the received action is not a type of "Phone_State", ignore it
if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
// Else, try to do some action
else
{
// Fetch the number of incoming call
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
// Check, whether this is a member of "Black listed" phone numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context);
// return;
}else{
disconnectPhoneItelephony(context);
sendSMS(context);
}
}
}
public void sendSMS(Context context) {
try{
String message = SharedPrefActivity.CommonMethod.getPrefsData(context, SharedPrefActivity.Constants.TextMessage, "");
Intent intent=new Intent(context,CallBarring.class);
PendingIntent pi=PendingIntent.getActivity(context, 0, intent,0);
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(number, null, message, pi,null);
Toast.makeText(context, "SMS Sent", Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(context, "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
// Method to disconnect phone automatically and programmatically
// Keep this method as it is
#SuppressWarnings({ "rawtypes", "unchecked" })
private void disconnectPhoneItelephony(Context context)
{
ITelephony telephonyService;
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try
{
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Try This:
// Check, whether this is a. member of "Black listed" phone. numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context);
// return;
}else{
disconnectPhoneItelephony(context);
sendSMS(context);
}
This to
// Check, whether this is a. member of "Black listed" phone. numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context)
}
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 am doing one SMS App.. in which i am trying to validate edittext(i.e. if user will not enter the phone number then it will show please enter the number), but it is not executing those part.. Please check it and give me some suggestion....My code is here.. Thank you in advance..
public class MainActivity extends Activity {
Button btnSend;
EditText txtPhoneNo;
EditText txtSMS;
String phoneNo, SMS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSend=(Button) findViewById(R.id.buttonSend);
txtPhoneNo=(EditText) findViewById(R.id.editTextPhoneNo);
txtSMS=(EditText) findViewById(R.id.editTextSMS);
btnSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
phoneNo=txtPhoneNo.getText().toString();
SMS=txtSMS.getText().toString();
if(phoneNo.equals(" ") || phoneNo == null) {
Toast.makeText(getBaseContext(), "Please Enter the number !", Toast.LENGTH_LONG).show();
}
else {
MyAsync ma = new MyAsync();
ma.execute();
}
}
});
}
class MyAsync extends AsyncTask<Void,Void,Void> {
ProgressDialog pd;
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "Wait!", "Sending...");
}
#Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try {
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, SMS, null, null);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS faild, please try again later!",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.cancel();
Toast.makeText(getApplicationContext(),"Sent",Toast.LENGTH_LONG).show();
}
}
}
You are using the following code to determine whether the user entered anything—
phoneNo.equals(" ") || phoneNo == null
Firstly, when the user keeps the EditText empty, it will be a 0-length string "" and NOT a space " ", which you're testing for.
Secondly, why are you making the null check? Is it possible that your layout won't have that EditText field? And, if that's possible, the null check should be the first thing to check. Because, if it can be null then your app will certainly crash, since you're testing for equals() on a null object before the null check!
If you want to try something more, don't check validation on click button.
Do it before with: TextWatcher
Your activity implements android.text.TextWatcher interface
You add TextChanged listeners to you EditText boxes
txt1.addTextChangedListener(this);
Of the overridden methods, you could use the afterTextChanged(Editable s) method as follows:
#Override
public void afterTextChanged(Editable s) {
// validation code goes here
}
You could directly check the contents of the EditText boxes like
String txt1String = txt1.getText().toString();
// Validate txt1String
To do this no need to take String and pass the content of edit-text into string. You can directly check by following code.
if(txtPhoneNo.length()<=0)
{
Toast.makeText(getBaseContext(), "Please Enter the number !", Toast.LENGTH_LONG).show();
}
else {
MyAsync ma = new MyAsync();
ma.execute();
}
Instead of these code,
if(phoneNo.equals(" ") || phoneNo == null) {
Toast.makeText(getBaseContext(), "Please Enter the number !", Toast.LENGTH_LONG).show();
}
Try following code,
// Trim the variable
phoneNo=txtPhoneNo.getText().toString().trim();
// Then condition part
if(phoneNo.length() == 0 && phoneNo != null) {
Toast.makeText(getBaseContext(), "Please Enter the number !", Toast.LENGTH_LONG).show();
}
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 created sms application android
I've been able to sms application smoothly but send by SIM 1 if i send with 2 SIM application it will error and there were no reports sent how if i want to do sms sending with 2 SIM smoothly
please help what should I add the source code of its
my code
public class MainActivity extends Activity {
Button sendBtn;
EditText txtphoneNo;
EditText txtMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
txtMessage = (EditText) findViewById(R.id.editTextSMS);
sendBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});
}
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();
}
}
#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;
}
}
The Android SDK has no support for dual-SIM devices. You will need to contact your device manufacturer to determine if and how you can send an SMS using the second SIM.