This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am coding a prog about SMS,i have a service,and a broadcast receiver,when a sms is sent,i get the "displayoriginatingadress,then its automaticly Resend another sms to the source number,but i have a bug/force close for Invalid Destination Address beacause when i get the source number(i am from france) its like this : "+33617890922" but i need to get it like this "0617890922",how can i do it ?
** Any Help????
public void onReceive(Context c, Intent intent) {
// TODO Auto-generated method stub
mContext = c;
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
Toast.makeText(c, "Service SMS Started OK !", Toast.LENGTH_LONG).show();
Toast.makeText(c, "SMS Received : "+messages.getMessageBody(),
Toast.LENGTH_LONG).show();
String str = messages.getMessageBody();
String num = messages.getDisplayOriginatingAddress();
String str1= PhoneNumberUtils.formatNanpNumber(num);
if(messages.getMessageBody().contentEquals("klmj")){
if (intent.getAction().equals(ACTION_RECEIVE_SMS)){
Intent serviceIntent = new Intent(mContext, ServiceLocation.class);
serviceIntent.putExtra("id", str1);
mContext.startService(serviceIntent);
}
.... .... .... ....
ok the problem is not from "+33" beacause i tried to replace the destinationadress to send sms by "+33677890098" and its work,im lost,it is the data i get from my broadcast receiver are corrupted?? here some code Also i dunno how to use an external library that i have found called "phoneutil" i think,is my problem come from my received number from my intent? i tried to put quote with no luck,also same problem using some String operation... wtf please help thanks you
UPDATE: i noticed that the String "str" is Empty when used in the "lo" method,see code modification below :
public class ServiceTe extends Service{
private final String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED";
private String initData;
int myLatitude , myLongitude;
private String loc;
private String str;
private String num;
public void onCreate(){
lo();
}
public int onStartCommand(Intent itn, int num1, int flag){
str = itn.getStringExtra("id");
Toast.makeText(this, "One !" +str, Toast.LENGTH_LONG).show();
return 0;
}
public void lo(){
// TODO Auto-generated method stub
Toast.makeText(this, "Two !", Toast.LENGTH_LONG).show();
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
//This Toast show : "" , Nothing...
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
if(RqsLocation(cid, lac)){
loc = (
String.valueOf((float)myLatitude/1000000)
+ " : "
+ String.valueOf((float)myLongitude/1000000));
Its doesnt work like this,Force Close , and invalid DestinationAddress error
SmsManager.getDefault().sendTextMessage(str OR this : itn.getStringExtra("id"); , null, loc, null, null);
}else{ Its work like this !
SmsManager.getDefault().sendTextMessage("+33643598356", null, "Cant Report Location", null, null);
};
OK i Fixed the Problem thanks anyways for your 'help,look code below:
public class ServiceTe extends Service{
private final String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED";
private String initData;
int myLatitude , myLongitude;
private String loc;
private String str;
private String num;
public void onCreate(){
}
public int onStartCommand(Intent itn, int num1, int flag){
str = itn.getStringExtra("id");
Toast.makeText(this, "One !" +str, Toast.LENGTH_LONG).show();
//Call here lo with str as argument:
lo(str);
return 0;
}
//Added a string here to receive the Data String from the intent(str)...
public void lo(String num){
// TODO Auto-generated method stub
//Thats all i have my data from my intent avaible here !
Toast.makeText(this, "Two !", Toast.LENGTH_LONG).show();
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
if(RqsLocation(cid, lac)){
loc = (
String.valueOf((float)myLatitude/1000000)
+ " : "
+ String.valueOf((float)myLongitude/1000000));
Its doesnt work like this,Force Close , and invalid DestinationAddress error
SmsManager.getDefault().sendTextMessage(str, null, loc, null, null);
}else{ Its work like this !
SmsManager.getDefault().sendTextMessage("+33643598356", null, "Cant Report Location", null, null);
};
Related
Good day everyone.
I would like to make an application which replies to received SMS automatically.
For example.
Jon Doe sends me - "Hi", Application gets the message body, checks it with my database where I have a potential response:
ID | Text | Potential Answer
01 | Hi | Hello how are you?
and Application sends the Potential response.
So far what I have achieved -
App receives the Message, checks it with the database ( using Like '%') and gets the correct "Potential Answer" Column and passes it as message text body, but to send it I am using a button.
My Reciever is a sperate file class
public class MyReceiver extends BroadcastReceiver {
public static String textSmsbody="";
private static final String TAG=MyReceiver.class.getSimpleName();
public static final String pdu_type="pdus";
#TargetApi(Build.VERSION_CODES.M)
#Override
public void onReceive(Context context, Intent intent) {
// Get the SMS message.
Bundle bundle = intent.getExtras();
SmsMessage[] msgs;
String strMessage = "";
String format = bundle.getString("format");
// Retrieve the SMS message received.
Object[] pdus = (Object[]) bundle.get(pdu_type);
if (pdus != null) {
// Check the Android version.
boolean isVersionM =
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M);
// Fill the msgs array.
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
// Check Android version and use appropriate createFromPdu.
if (isVersionM) {
// If Android version M or newer:
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
} else {
// If Android version L or older:
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
// Build the message to show.
String a=msgs[i].getMessageBody();
textSmsbody=msgs[i].getMessageBody();
if (a.contains("?")) {
strMessage=msgs[i].getOriginatingAddress();
// strMessage += " :" + msgs[i].getMessageBody() + "\n";
}
else {
strMessage=a;
// strMessage += "SMS from" + msgs[i].getOriginatingAddress();
// strMessage += "ELSE:" + msgs[i].getMessageBody() + "\n";
}
// Log and display the SMS message.
Log.d(TAG, "onReceive: " + strMessage);
Toast.makeText(context, strMessage, Toast.LENGTH_LONG).show();
}
}
}
}
Sending method is in my MainActivity.
public void smsSendMessage(View view) {
databaseSearch();
// Set the destination phone number to the string in editText.
String destinationAddress = "2020";
// Find the sms_message view.
// Get the text of the SMS message.
String smsMessage = sendingText;
// Set the service center address if needed, otherwise null.
String scAddress = null;
// Set pending intents to broadcast
// when message sent and when delivered, or set to null.
PendingIntent sentIntent = null, deliveryIntent = null;
// Use SmsManager.
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage
(destinationAddress, scAddress, smsMessage,
sentIntent, deliveryIntent);
}
In layout I have a button which calls smsSendMessage () ;
My question is how I can make it automatically without button.
When the phone receives a message, the app shall check it with the database and send it by itself.
Please tell me if you need to see my Manifest file, or databasehelper.
Using JobService should be a suitable option in your case.
Create a JobService class like that
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ExampleJobService extends JobService {
#Override
public boolean onStartJob(JobParameters params) {
//send a message
return true;
}
#Override
public boolean onStopJob(JobParameters params) {
return true;
}
}
Also Declare In your Manifest
<service
android:name=".ExampleJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
Now in your Receiver, you can start it like that
ComponentName componentName = new ComponentName(context, ExampleJobService.class);
PersistableBundle bundle = new PersistableBundle();
bundle.putLong("lat", lat);
bundle.putLong("lon", lon);
JobInfo jobInfo = new JobInfo.Builder(0, componentName)
.setExtras(bundle)
.build();
For more details about JobServices https://www.vogella.com/tutorials/AndroidTaskScheduling/article.html
So i'm working on a WhatsApp like verification system where user inputs the code received via sms and code is sent back to the server..yada yada ..all that basic stuff.
My dilemma is that i have received and read the sms correctly. How do i filter the body so that that it passes the number (not phone number but verification code) to the editText automatically. I'm trying to avoid users having to enter the verification code manually. Lemme show some code below.
public void processReceive(Context context, Intent intent){
Bundle bundle = intent.getExtras();
if(bundle == null){
return;
}
Object[] objectArray = (Object[])bundle.get("pdus");
for(int i = 0; i < objectArray.length; i++){
SmsMessage smsMsg = SmsMessage.createFromPdu((byte[])objectArray[i]);
String smsBody = smsMsg.getMessageBody();
Toast.makeText(context, smsBody, Toast.LENGTH_SHORT).show();
}
}
//In the code above, my broadcastReceiver receives the sms and i can display the body in a toast. The sms goes something like this: "Your verification code: 12345".
How do i get just the code from the sms and send its value to and editText programmatically like WhatsApp does.
number = (EditText) findViewById(R.id.number);
Thank you. You input is greatly appreciated
Try this it may be help to you
public static String GENERAL_OTP_TEMPLATE = "Your verification code: (.*).";
SmsMessage[] message = new SmsMessage[objectArray.length];
for (int i = 0; i < objectArray.length; i++) {
message[i] = SmsMessage.createFromPdu((byte[]) objectArray[i]);
}
Pattern generalOtpPattern = Pattern.compile(GENERAL_OTP_TEMPLATE);
Matcher generalOtpMatcher = generalOtpPattern.matcher(message[0].getMessageBody().toString());
if (generalOtpMatcher.find()) {
String otp = generalOtpMatcher.group(1);
number.setText(otp);
}
You can just use Split function to get your last 5 numbers :
String[] splited = body.split(":");
String mylastnum= splited[1];
number.settext(mylastnum);
hope it help !
register Broad cast Receiver in android manifest using below mention code.
<!-- SMS Receiver -->
<receiver android:name=".receiver.SmsReceiver">
<intent-filter android:priority="99999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
Create Broad Cast Receiver using below mention code.
#Override
public void onReceive(Context context, Intent intent) {
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
for (Object aPdusObj : pdusObj) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) aPdusObj);
String senderAddress = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
Log.e(TAG, "Received SMS: " + message + ", Sender: " + senderAddress);
// if the SMS is not from our gateway, ignore the message
if (!senderAddress.toLowerCase().contains(Config.SMS_ORIGIN.toLowerCase())) {
Log.e(TAG, "SMS is not for our app!");
return;
}
// verification code from sms
String verificationCode = getVerificationCode(message);
Log.e(TAG, "OTP received: " + verificationCode);
Intent hhtpIntent = new Intent(context, HttpService.class);
hhtpIntent.putExtra("otp", verificationCode);
context.startService(hhtpIntent);
}
}
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
Parse Verification Code that come through SMS.
private String getVerificationCode(String message) {
String code = null;
int index = message.indexOf(Config.OTP_DELIMITER);
if (index != -1) {
int start = index + 2;
int length = 6;
code = message.substring(start, start + length);
return code;
}
return code;
}
Hi I'm trying to get names and phone numbers from contact
this is the class which extends BroadcastReceiver
public class TextMessageReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent)
{
Bundle bundle=intent.getExtras();
Object[] messages=(Object[])bundle.get("pdus");
SmsMessage[] sms=new SmsMessage[messages.length];
for(int n=0;n<messages.length;n++){
sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
}
for(SmsMessage msg:sms){
//MainActivity.
String num = getcont( MainActivity.,msg.getOriginatingAddress());
MainActivity.updateMessageBox("Message de la part: "+msg.getOriginatingAddress()+"\n"+
"Qui vous dit: "+msg.getMessageBody()+"\n");
}
}
In the Onrecieve methode I get the sms and the the one who send it,but I'd like to get the name not the number after some researche I found this way:
public static String getcont (Context context ,String num)
{
Cursor people = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String numbr = null;
while(people.moveToNext()) {
int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = people.getString(nameFieldColumnIndex);
int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
String number = people.getString(numberFieldColumnIndex);
if (number.equals(num)) {
numbr = contact;
}
}
people.close();
return numbr;
}
I have an Activity called MainActivity I try to call the method getcont()
String num = getcont( MainActivity ,msg.getOriginatingAddress());
but it show me this error
MainActivity cannot be resolved to a variable
theres an other way to call getContentResolver().query() and thnx for helping me
How about using an intent to navigate to MainActivity?
if I fully understood your requiremnet ...
Instead of doing
String num = getcont( MainActivity ,msg.getOriginatingAddress());
why not do something like this?
Intent ii=new Intent(MainActivity.this, newclass.class);
ii.putExtra("name", s);
startActivity(ii);
I'm a beginner in Android development. What I'm trying to create is an app the speaks to you then waits for your answer so its kind of text to speech works ,then it activates speech recognition and with my code the text to speech works then it calls speech recognition.Then an erro msg is shown ,the catch block is executed.
The problem is that I have to add Speech Recognition in a separate class, then add an Object of it in the Adapter not Main Activity. Speech Recognition won't work and since all tutorials for speech recognition for Android are adding the code in Main Activity and I'm doing it as class called in the adapter is the problem ?
UPDATE startActivityForResult() method is not working
Here is the code for SpeechRecogntion class
public class SpeechRecog extends Activity {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;
String textResult ;
public void Start (Intent i , Context c){
i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
i.putExtra(RecognizerIntent.EXTRA_PROMPT, "Are You Done , yet? ");
try{
startActivityForResult(i,VOICE_RECOGNITION_REQUEST_CODE);
} catch(Exception e) {
Toast.makeText(c, "SpeechRecogntion is not avalible on your device ", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == VOICE_RECOGNITION_REQUEST_CODE)&& (resultCode== RESULT_OK)) {
textResult= data.getStringExtra(RecognizerIntent.EXTRA_RESULTS);
if (checkResult())
Toast.makeText(getApplicationContext(), "You are done ", Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), "You are not done ", Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
private boolean checkResult() {
if (textResult.equals("done")||(textResult.equals("yes")))
return true ;
return false;
}
}
And Here is the object of it in the Adapter class
private SpeechRecog mySP;
private Intent mySpIntetn ;
public ListAdapterClass(Context context) {
mytts = new TxtToSpeechClass(context);
mySP = new SpeechRecog();
}
And Here is where my problem relies ,the on clicklistener inside the Adapter class
Lh.btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if (Lh.btn.getText().equals("Play"))
{
Lh.btn.setText("Pause"); //change the btn status so user know how to stop it
List<Items> hResults=db.getAllItems(Lh.idList + 1);
for (int i = 0; i < hResults.size(); i++)
{
Items item = hResults.get(i);
String s = item.getItemname();
mytts.Talk(s,1);
mySP.Start(mySpIntetn , context ); //the code in here wont work is there something I'm missing here
Toast.makeText(context,s, Toast.LENGTH_SHORT).show();
}
mytts.Talk("Congrats ",1);// 1 means Queue_add
} else {
Lh.btn.setText("Play");
mytts.Talk("See you later", 0); // 0 means Queue_Flush
}
}
);
mySpIntetn is private. Try to change it to public in the Adapter class.
I am trying to get contacts from call log. I can get the contact numbers from main contacts using this code :
public void getContacts(View view) {
Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intentContact, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
try {
to.setText(getContactInfo(intent));
} catch(NullPointerException e) {
// Do nothing ;)
}
}
}
protected String getContactInfo(Intent intent)
{
String phoneNumber = to.getText().toString();
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
while (cursor.moveToNext())
{
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
if(phoneNumber.endsWith(">"))
phoneNumber += ", "+name;
else
phoneNumber += name;
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
{
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
{ phoneNumber = phoneNumber + " <" + phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+">";
}
phones.close();
}
}
cursor.close();
return phoneNumber;
}
What this does is when we click a "Contact" button it open a list with all the contacts, the user can select any contact and that selected contact will be added in the "To" field. I want to do the exactly same thing, but instead of displaying all the contacts i want to display only those who were recently used (call log) for selection.
Also it would be nice if you can tell how to do this with groups also.
I got this going using my own version. i used a dialog and handed it the cursor to the call log. Here is the function:
public void getCallLog() {
String[] callLogFields = { android.provider.CallLog.Calls._ID,
android.provider.CallLog.Calls.NUMBER,
android.provider.CallLog.Calls.CACHED_NAME /* im not using the name but you can*/};
String viaOrder = android.provider.CallLog.Calls.DATE + " DESC";
String WHERE = android.provider.CallLog.Calls.NUMBER + " >0"; /*filter out private/unknown numbers */
final Cursor callLog_cursor = getActivity().getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, callLogFields,
WHERE, null, viaOrder);
AlertDialog.Builder myversionOfCallLog = new AlertDialog.Builder(
getActivity());
android.content.DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int item) {
callLog_cursor.moveToPosition(item);
Log.v("number", callLog_cursor.getString(callLog_cursor
.getColumnIndex(android.provider.CallLog.Calls.NUMBER)));
callLog_cursor.close();
}
};
myversionOfCallLog.setCursor(callLog_cursor, listener,
android.provider.CallLog.Calls.NUMBER);
myversionOfCallLog.setTitle("Choose from Call Log");
myversionOfCallLog.create().show();
}
You can use ContactsContract.Contacts.CONTENT_STREQUENT_URI which will give you both Frequently called and Starred contacts.
From API 21 is possible to use this: https://developer.android.com/reference/kotlin/android/provider/CallLog.Calls#CACHED_LOOKUP_URI
CACHED_LOOKUP_URI added in API level 21 static val CACHED_LOOKUP_URI:
String The cached URI to look up the contact associated with the phone
number, if it exists.
This value is typically filled in by the dialer app for the caching
purpose, so it's not guaranteed to be present, and may not be current
if the contact information associated with this number has changed.