private void sendSMS(){
String phoneNo = number.getText().toString().trim();
String SMS = message.getText().toString().trim();
if (number.getText().toString().length() >= 4) {
if (!message.getText().toString().equals("")) {
SmsManager smsManager =sManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, SMS, null, null);
Toast.makeText(this, "Your SMS is sent successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Enter a message!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "The number is incorrect, it must contain at least 4 numbers!", Toast.LENGTH_SHORT).show();
}
}
I have this piece of code that works successfully! But I want now sent message to various numbers with separate ";".
For example, in the emulator I want enter in the numbers zone (1254;2058;153348) and the message has to be sent to all the contacts I have enter.
If phoneNo contains the following value: 1254;2058;153348
You can split your phone numbers to array with numbers.
Please see the following example:
String phoneNo = number.getText().toString().trim();
String []phoneNumbers = phoneNo.split(";");
After that you can iterate by every element in phoneNumbers array
I want just sent the same message at various numbers.
Why dont you split string on ; and than iterate through loop to send it one by one
I hope you understand what i mean
Related
While I was trying to add the data to firebase database I found that the keys are auto generated. I want to customize key value according to the ID value user input.
This is the code I implemented to save data into database. This code is working with auto-generated key value:
btn6.setOnClickListener(new View.OnClickListener() { //create
#Override
public void onClick(View view) { //Create
dbref = F.irebaseDatabase.getInstance().getReference().child("DetailsOfCode");
try {
if (TextUtils.isEmpty(description.getText().toString()))
Toast.makeText(getApplicationContext(), "Please enter a Description", Toast.LENGTH_SHORT).show();
else if (TextUtils.isEmpty(code.getText().toString()))
Toast.makeText(getApplicationContext(), "Please enter a Code", Toast.LENGTH_SHORT).show();
else if (TextUtils.isEmpty(id.getText().toString()))
Toast.makeText(getApplicationContext(), "Please enter an Id", Toast.LENGTH_SHORT).show();
else {
DCode.setDescription(description.getText().toString().trim());
DCode.setCode(code.getText().toString().trim());
DCode.setId(id.getText().toString().trim());
dbref.push().setValue(DCode); //Here I tried to set customize Key, but always showing error
Toast.makeText(getApplicationContext(), "Data Saved Successfully", Toast.LENGTH_SHORT).show();
clearControls();
}
} catch (NumberFormatException e) {
Toast.makeText(getApplicationContext(), "Invalid Data, check again", Toast.LENGTH_SHORT).show();
}
}
});
I want to get the key value as the "ID" user input.
DCode.setId(id.getText().toString().trim()); <-- This is the id session and I want this ID to show as the key.
If user enter ID as "1", the key value also want to show as "1". I don't want to hard code the key as"1", "2" for each data input. I want it to implement the key according to ID value.
In here I want id:1 as Key:1 .Like this,
- DetailsOfCode
- 1
code: "cS3j2A"
description: "Sample 1"
id: "1"
I'm trying to validate two strings via TextUtils.isEmpty but i'm failing everytime.
Following is my code:
private void addArtist() {
//getting the values to save
String email = editTextName.getText().toString().trim();
String mobileno = editTextName1.getText().toString().trim();
//checking if the value is provided
if (TextUtils.isEmpty(email)){
//if the value is not given displaying a toast
Toast.makeText(this, "Please enter email.", Toast.LENGTH_LONG).show();
}
else{
//getting a unique id using push().getKey() method
//it will create a unique id and we will use it as the Primary Key for our Artist
String id = databaseArtists.push().getKey();
//creating an Artist Object
Artist artist = new Artist(id, email, mobileno);
//Saving the Artist
databaseArtists.child(id).setValue(artist);
Toast.makeText(this, "Successful...", Toast.LENGTH_LONG).show();
}
}
Currently this code is working on string email, i want it to work with string mobileno also.Any help is appreciated.
Why don't you just try to use try catch block and inside it try to convert this string after you read from TextField control object into number if "Mobileno" variable should be int type? If it fails then you can in catch block make some other instructions to prevent from inserting it to field of specific object. Have you tried that?
This question already has answers here:
Convert ArrayList to String array in Android
(7 answers)
Closed 8 years ago.
I am trying to send an email, from a button click, to all emails stored in a sqlite database. I have been successful in selecting one email, but now i am trying to use a cursor to continue to send the email to all stored email addresses. Below is the button call and the method to retrieve the array of addresses from the database.
view.findViewById(R.id.btn_save).setOnClickListener(new OnClickListener() {
public void onClick(View view ) {
Mail m = new Mail("gmail#gmail.com", "pw");
String[] usereMail = getEmailsFromDB().split(",");;
m.setTo(usereMail);
m.setFrom("iwalker77#gmail.com");
m.setSubject("Never going to happen");
m.setBody("If you receive this email, the planets have aligned and i have somehow managed to get an email sent to all players in the database");
try {
if(m.send()) {
Toast.makeText(getActivity(), "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
}
}
private ArrayList<String> getEmailsFromDB() {
// TODO Auto-generated method stub
dataBase = mHelper.getReadableDatabase();
Cursor Cursor = dataBase.rawQuery("SELECT " + DbHelper.KEY_EMAIL + " FROM "
+ DbHelper.TABLE_NAME, null);
ArrayList<String> array = new ArrayList<String>();
while(Cursor.moveToNext()) {
String usereMail = Cursor.getString(Cursor.getColumnIndex(DbHelper.KEY_EMAIL));
array.add(usereMail);
}
Cursor.close();
return array;
}
});
The error i am receiving is on the line ' String[] usereMail = getEmailsFromDB().split(",");' and it is due to the error 'Type mismatch: cannot convert from ArrayList to String[]'. Is there any way round this? And if not, how should i change my approach?
Clearly the ArrayList<String> array and String[] are incompatible types
Use toArray to return an array of the type contained within the collection and match the expected return type for the method getEmailsFromDB
return list.toArray(new String[array.size()]);
I am new to android and am creating a registration page. Since android doesn't support mysql , i created a php based script which gives out integer values for different queries.
2 --> successful insertion in database
-1 --> Email already used
0--> username used
Depending on the integer value of the response that I get from php script I do the subsequent tasks. Even though successfully inserting the values in database and the first print stament shows the values of Regresult as "2", the if statement is not working and the both else if statements are working fine.
/* Getting the response back from the PHPAPI and storing it in a variable */
HttpEntity entity = Response.getEntity();
String Regresult = EntityUtils.toString(entity);
/* printing value of Regresult to verify if the condition is right or wrong */
System.out.println(Regresult);
/* Registration depending upon response from Http post call */
/* Email used works fine when Regresult = -1 */
if(Regresult.equals("-1"))
{
Context text = getApplicationContext();
CharSequence message = "Email already in use.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(text, message, duration);
toast.show();
}
/* Username used work fine when Regresult = 0 */
else if(Regresult.equals("0"))
{
Context text = getApplicationContext();
CharSequence message = "Username already in use.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(text, message, duration);
toast.show();
}
/* Here is the problem when Regresult= 2 , even though consition is correct the statement don't get executed */
else if(Regresult.equals("2"))
{
Context text = getApplicationContext();
CharSequence message = "Registration Successful.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(text, message, duration);
toast.show();
System.out.println(Regresult);
}
// I think it may be some spacing problem so try this way
if(Regresult.trim().equals("-1")){
Context text = getApplicationContext();
CharSequence message = "Email already in use.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(text, message, duration);
toast.show();
}else if(Regresult.trim().equals("0")){
Context text = getApplicationContext();
CharSequence message = "Username already in use.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(text, message, duration);
toast.show();
}else if(Regresult.trim().equals("2")) {
Context text = getApplicationContext();
CharSequence message = "Registration Successful.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(text, message, duration); toast.show();
}
I want to filter messages with the prefix "kmsmks", compile success but I get force close:
...
String firstword = "kmsmks";
while (cursor.moveToNext())
{ String address = cursor.getString(1);
String body = cursor.getString(3);
String firstmessageword = body.substring(0,7);
if (firstword.equals(firstmessageword))
{
System.out.println("======> Mobile number => "+address);
System.out.println("=====> SMS Text => "+body);
sms.add("Address=> "+address+"\n SMS => "+body);
}
}
return sms;
}
…
original code : http://www.technotalkative.com/android-fetch-inbox-sms/
Make sure you have added android.permission.READ_SMS in manifest