I'm sending an SMS from my app using an Intent. The SMS pulls a string with "\n" and "\t" formatting out of a text view to send out to a contact of the user's choosing.
My problem is that when sending from a device to another device, the SMS only sends the first 15 characters of the message! When the message appears on the first device, it has all the characters in the string. Then only the first 15 of those characters are received by the second device.
// SMS Activity
public void sendSMS(View v) {
mess = ""+Summary.getText().toString();
Uri uri = Uri.parse("smsto:");
Intent txt = new Intent(Intent.ACTION_SENDTO, uri);
txt.putExtra("sms_body", mess);
startActivity(txt);
}
This is how I generated the string for the message. The text view is also being used for showing the string in the App.
for(...){
if{...}
else{
String beer = name[counter];
int amount = amt[counter];
inText += "x"+ amount+ "\t" + beer + "\n";
}
}
Summary.setText(""+inText);
mess = ""+Summary.getText().toString();
Is there a different method to go about sending this string through sms? I know I can do it using SmsManager, but I want to be able to use the phones messaging system (easier because they can choose the contacts). Any examples I can follow?
Could I use a cursor to get the information for the string?
try adding txt.setType("text/plain");
this might solve your problem.
Related
I want use register in my application and i should send password and verifyCode with SMS to users phones.
But i should read verifyCode from message and set automatically number into verifyCode EditText.
My message format :
Hi, welcome to our service.
your password 12345
your verifyCode 54321
How can i do it? Please help me <3
Assuming that the number of digits are fixed in password and verify codes (Generally they are same as default values), We can extract digits from the string and then find substring which has verify code. This assumption is for simplicity.
String numberOnly= str.replaceAll("[^0-9]", "");
String verifyCode = numberOnly.substring(6);
Here String verifyCode = numberOnly.substring(6); is getting last 5 digits of the string which is your verification code. You can also write numberOnly.substring(6,10); to avoid confusions.
But this is prone to errors like StringIndexOutOfBoundsException, So whenever you want to get substring which is starting from index i till the end of the string, always write numberOnly.substring(i).
There are a lot ways to do this. You can use some complicated regex or use a simple spilt method.
Try this,
String str = "Hi, welcome to our service.\n"
+ "\n"
+ "your password \n"
+ "12345\n"
+ "\n"
+ "your verifyCode \n"
+ "54321";
// Solution #1
String[] parts = str.split("\n");
System.out.println(parts[3]);
System.out.println(parts[6]);
// Solution #2
String PAT = "(password|verifyCode)\\s+(\\d+)";
Pattern pats = Pattern.compile(PAT);
Matcher m = pats.matcher(str);
while (m.find()) {
String grp = m.group(2);
System.out.println(grp);
}
I am using the following code to change the phone number of an outgoing call under a certain condition. The code seems to be working. The toast appears with the old and new number but the phone dialer shows the old number in the dialing screen.
If I check the call history details after the call however, it shows the new number.
How can I get the dialer screen to show the updated number?
public void onReceive(Context context,Intent intent) {
final String oldNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String modNumber = "";
if (oldNumber.startsWith("xx")) {
modNumber = oldNumber.replace("xx", "yyy");
this.setResultData(modNumber );
final String newNumber = this.getResultData();
String msg = "Call Intercepted. Old number " + oldNumber + ", new number " + newNumber;
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
}
}
The android.permission.PROCESS_OUTGOING_CALLS is present in the manifest file.
App is deployed on a phone running Android 5.0.2 (Lollipop)
I am trying to extract some data from an android notification.
When I receive a mail, I can get the name of the sender from the title attribute and the rest of the notification is in the text attribute with the following code:
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
if (sbn.getPackageName().equals("com.google.android.gm") {
String title = sbn.getNotification().extras.getString(Notification.EXTRA_TITLE);
String text = "";
text = sbn.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString();
Log.i(TAG, "Title: " + title);
Log.i(TAG, "Text: " + text);
}
Given now the following screenshot of a notification.
Android mail notification
The title is no longer the name of the sender (but 6 new messages) so I thought to take the text below and extract the name, but my code from above doesn't work because of an NullPointerException in line of text = sbn.getNotification()...
What am I missing or doing wrong? Does anyone know how to get the whole text or even a nice method to get the senders name?
Cheers
i wanna make an action that the android app can send an email, and the email subject and body already fill with the edittext from the android app.
here's my code
Intent ii = new Intent(android.content.Intent.ACTION_SEND);
ii.setType("text/plain");
EditText txtName = (EditText)findViewById(R.id.txtName);
EditText txtAddress = (EditText)findViewById(R.id.txtAddress);
EditText txtLatitude = (EditText)findViewById(R.id.txtLatitude);
EditText txtLongitude = (EditText)findViewById(R.id.txtLongitude);
ii.putExtra(android.content.Intent.EXTRA_SUBJECT, txtName.getText().toString());
ii.putExtra(android.content.Intent.EXTRA_TEXT, txtAddress.getText().toString());
ii.putExtra(android.content.Intent.EXTRA_TEXT, txtLatitude.getText().toString());
ii.putExtra(android.content.Intent.EXTRA_TEXT, txtLongitude.getText().toString());
startActivity(Intent.createChooser(ii, "Share via"));
The subject is fine, but the body email is only fill with the last content(txtLongitude).
please tell me what to do.
Gratz.
You need to first make one string from the 3 EditTexts, then set it as an extra. For Example:
String body = txtName.getText().toString() + txtLatitude.getText().toString() + txtLongitude.getText().toString()
ii.putExtra(android.content.Intent.EXTRA_TEXT, body);
You'll probably also want to add some things like new line characters to format the text.
The subject is fine, but the body email is only fill with the last content(txtLongitude).
That is because you keep overriding the same EXTRA_TEXT extra. Call putExtra() for EXTRA_TEXT once, with the complete body of the email you want to send.
So using google places reference (detailed web-service) i retrieved a "formatted phone number" its in the form of (256) 922-0556. The goal is to call this number. The way I am trying is be using an intent. However, the number above is not a in the format to use Uri parse method. Anyone know a solution to call this number? Is there a different intent or a good way to turn this into Uri data? I have seen the opposite of this done like so:
1234567890 → (123) 456-7890
String formattedNumber = PhoneNumberUtils.formatNumber(unformattedNumber);
But i want to do the reverse of this. any ideas or alternative solutions?
Here is my code:
protected void onPostExecute(Boolean result){
Intent callintent = new Intent(Intent.ACTION_CALL);
callintent.setData(Uri.parse(phoneNum));
try {
startActivity(callintent);
}catch (Exception e) {e.printStackTrace();}
}
Where phoneNum is a formatted phone number string retrieved from GooglePlaces via JSON
To expand on Peotropo's comment: is there a better way to replace values than the following?
phoneNum = phoneNum.replace(" ", ""); // gets rid of the spaces
phoneNum = phoneNum.replace("-", ""); // gets rid of the -
phoneNum = phoneNum.replace("(", ""); // gets rid of the (
phoneNum = phoneNum.replace(")", ""); // gets rid of the )
This is simple string. Use String.replace() method to remove extra chars.
You can also use replaceAll method:
String phoneNumber = "(123)123-456465"
return phoneNumber.replaceAll("[^0-9]", "");
Not tested docs are here:
replaceAll
Java regular expressions
You don't need to do a string replace. You can use the Spannable code below to have your phone automatically recognize the number and call it. It adjusts for parentheses, spaces and dashes.
// call the phone
SpannableString callphone = new SpannableString("Phone: " + phone);
callphone.setSpan(new StyleSpan(Typeface.BOLD), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
callphone.setSpan(new URLSpan("tel:"+phone), 7, 21, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView zphone = (TextView) findViewById(R.id.phone);
zphone.setText(callphone);
zphone.setMovementMethod(LinkMovementMethod.getInstance());
It will display
Phone: (123) 456-7890
Where you see 7,21 in the code above it is saying to start at the 8th character, which is the ( and end at the 21st character which is the last digit in the phone number. Adjust it to display how you want.
Nothing special to do in your view:
<!-- Phone Number Label -->
<TextView
android:id="#+id/phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"/>