Iam using Google Retriver API for automatic sms verification, now everthings works fine.
Iam receiving sms message from Broadcast receiver class.
This is my sms message..
<#> Waahan: Your verification code is:1453 jtN03jdhD6p
I want to extract only the otp from the message..
SMSBroadcastReceiver.java
public class SMSBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
switch(status.getStatusCode())
{
case CommonStatusCodes.SUCCESS:
String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
break;
case CommonStatusCodes.TIMEOUT:
break;
}
}
}
}
I have searched, but every one is using telephony.. iam using retrievel API..
Thanks in advance..
case CommonStatusCodes.SUCCESS:
String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
String OTP = message.substring(message.lastIndexOf(":") + 1);
possible using SubString(Start,End) Function
Related
newbie to android !
basically i trying to make a proprietary app for my product which runs on BT SPP Protocol !
so i decided use https://github.com/akexorcist/BluetoothSPPLibrary a class which handles bt service for outgoing / incoming data from bt device
Sending data is ok !
but when i trying receiving data simply cannot call / receive data inside activity class
here is my snippets of my code !
BTSpp.java
public interface OnDataReceivedListener {
public void onDataReceived(byte[] data, String message); /// for sending back data to the activity
}
...
//this is not executing cause log is not running when receives data
public void setOnDataReceivedListener (OnDataReceivedListener listener) {
Log.d("TAG", "setOnDataReceivedListener() returned: " );
if(null == mDataReceivedListener)
mDataReceivedListener = listener;
}
...
//Here is the snippet for reading data from bt service and
//it is showing log whenever i receives data from spp device
case BluetoothState.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String readMessage = new String(readBuf);
if(readBuf != null && readBuf.length > 0) {
if(mDataReceivedListener != null){
Log.d("TAG", "handleMessage() returned: "+mDataReceivedListener ); //executes
mDataReceivedListener.onDataReceived(readBuf, readMessage);}
}
break;
and in my activity window onCreate method
not at all executing this method
MainActivity.java
BluetoothSPP bt=new BluetoothSPP(this); //initialized
{...}
bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
#Override
public void onDataReceived(byte[] data, String message) {
Log.d(TAG, "onDataReceived() returned: " );
Log.i(TAG, "onDataReceived: "+message);
Log.i(TAG, "onDataReceived: "+data);
processIncomingdata(message);
}
});
maybe this function doesn't call from service class . i literally can't find any clue !
can anyone give me some suggestion about this !
Thank you in advance !
Hi trying to send SMS with android using below code.
public static void sendsmsstd(String number, String message, Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("vnd.android-dir/mms-sms");
intent.putExtra("address", number);
intent.putExtra("sms-body", message);
context.startActivity(intent);
}
Many sites have suggested it , but when code runs and SMS app opens, it choose right contact to send message but message body is always empty.
What else can be done to fix it. I m missing some code.
You're using sms-body. Change that to sms_body and that should fix it.
I am trying to use GCM for android. After some steps that are provided by developer.google i got the registration id in toast, Now i want to store it in database. How to change here to store it in database. What steps i need to change for this updation???
code
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
String token = intent.getStringExtra("token");
Toast.makeText(getApplicationContext(), "GCM registration token: " + token, Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(config.SENT_TOKEN_TO_SERVER)) {
// gcm registration id is stored in our server's MySQL
Toast.makeText(getApplicationContext(), "GCM registration token is stored in server!", Toast.LENGTH_LONG).show();
} else if (intent.getAction().equals(config.PUSH_NOTIFICATION)) {
// new push notification is received
Toast.makeText(getApplicationContext(), "Push notification is received!", Toast.LENGTH_LONG).show();
}
}
};
You can store the GCM token in SharedPreferences itself.
Here is small piece of code that you can add after receiving the token:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor e = sharedPreferences.edit();
e.putString("GCMTOKEN", token);
e.commit();
And to get the token you can use this,
String token = sharedPreferences.getString("GCMTOKEN", null);
I would recommend you to keep the token as a key-value pair in SharedPreference. You will be able to retrieve and save/update your registration ID with ease using it instead of creating a record in mySQL.
There is an implementation in the top answer in this question. Do take a look.
How to get RegistrationID using GCM in android
You can follow this sample for how to deal with push token registration and refreshing token as well which is an important part
https://github.com/ChamariaUmang/MoEngageDemo
I'm trying to send a Telegram message to a specific number from within my Android app. Right now my code launches Telegram app, and then the user has to select the destinatary. What I want to do is to send the message to the specified number, without having the user select the contact. My code is as follows:
/**
* Intent to send a telegram message
* #param msg
*/
void intentMessageTelegram(String msg)
{
final String appName = "org.telegram.messenger";
final boolean isAppInstalled = isAppAvailable(mUIActivity.getApplicationContext(), appName);
if (isAppInstalled)
{
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.setPackage(appName);
myIntent.putExtra(Intent.EXTRA_TEXT, msg);//
mUIActivity.startActivity(Intent.createChooser(myIntent, "Share with"));
}
else
{
Toast.makeText(mUIActivity, "Telegram not Installed", Toast.LENGTH_SHORT).show();
}
}
You can't send to special number, But You can do this by USERID
try {
Intent telegramIntent = new Intent(Intent.ACTION_VIEW);
telegramIntent.setData(Uri.parse("http://telegram.me/USERID"));
startActivity(telegramIntent);
} catch (Exception e) {
// show error message
}
This code will show user an alert for choosing applications that support telegram uri's like Telegram itself and Mobogram!
Tip: don't set package name. some people install telegram alternatives like mobogram.
The Telegram Android App does not have a way to send messages directly to telegram users, so if you use the share intent, you'll get what telegram / any other app wants to do with the message shared. In this case, open the contact list to send this message to him.
If you want to send messages directly to Telegram users you should use the Telegram API
https://core.telegram.org/api#getting-started
once you have configured your API key in your app, you could send messages, read them or even get the telegram contacts with these methods
https://core.telegram.org/methods
This one worked for me:
try {
Intent telegram = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/USER_NAME"));
telegram.setPackage("org.telegram.messenger");
startActivity(telegram);
}catch (Exception e)
{
Toast.makeText(getContext(), "Telegram app is not installed", Toast.LENGTH_LONG).show();
}
Tip: You can get USER_NAME by click on you telegram profile option you will get option of username in Account session --> if username is none create unique username and put here its work for me.
This one worked for me:
//check if application is installed first before running this code.
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://telegram.me/+UT_USER_ID_HERE"));
final String appName = "org.telegram.messenger";
i.setPackage(appName);
this.startActivity(i);
try the intent like this
tg://resolve?domain=YOUR_USER_ID
it's more direct then https://t.me
I'm using this for send message on background :
this cod is api telegram for use send message by url token bot
1 - use WebView on xml like this :
<WebView
android:id="#+id/webView_sendToTelegram"
android:layout_width="0dp"
android:layout_height="0dp"/>
2 - on java use this cod:
public static void SendMessageToBotTelegram(String chatID, String text, String botToken,WebView webView) {
webView.loadUrl(MessageFormat.format("https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}", botToken, chatID, text));
}
chatID : your telegram id #RawDataBot for get id
text : text your message
botToken : your bot for get message if you don't have bot using #BotFather for crate bot
webView : your id WebView on xml
I have an auto reply sms Android application I built and I don't want the auto reply (sent sms) to show in the default messaging app. I have searched and searched and couldn't find an answer. Is there a way to bypass writing the sent sms into the default messaging app?
Here my BroadcastReciever I am using to get the data and send out the message
public class SmsReceiver extends BroadcastReceiver {
ParseUser user = ParseUser.getCurrentUser();
// Auto reply message composed of the current reply and url from that business
String msg = user.getString("myCurrentReply") + " " + user.getString("couponUrlChosen");
List smsFromList = user.getList("smsFrom");
String userName = (String) user.get("username");
#Override
public void onReceive(final Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
final String pno = smsMessage[0].getOriginatingAddress();
user.put("lastSmsFrom", pno);
user.saveInBackground();
// show first message
Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
// Check Phone Number from SMS Received against Array in User Row
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
Log.d("Username: ", userName);
query.whereEqualTo("username", userName);
query.whereContainedIn("lastSmsFrom", smsFromList);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> smsList, ParseException e) {
if (e == null) {
Log.d("Errors", "none");
if (smsList.size() == 0) {
// Send SMS
sendSms(pno, msg);
// Add Phone number to smsFrom in currentUsers Row
user.addUnique("smsFrom", pno);
// Save Phone Number in Array
user.saveInBackground();
Log.d("List size: ", " " + smsList.size());
}
} else {
Log.d("Error Message: ",
e.getMessage());
}
Log.d("Already sent to this number today. ", " " + smsList.size());
}
});
}
private void sendSms(String phonenumber, String message) {
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(phonenumber, null, message, null, null);
}
}
Prior to KitKat, SMS sent using SmsManager require the app sending the message to insert it into the Provider, so it would just be a matter of omitting that.
Starting with KitKat, any app that is not the default SMS app and uses SmsManager to send messages will have the messages automatically written to the Provider for it by the system. There's no way to prevent this, and, furthermore, the app won't be able to delete those messages, either, as it won't have write access to the Provider.*
The app that is the default SMS app is responsible for writing its outgoing messages, so it would be able to omit that step. The system does no automatic writes for the default SMS app.
* There is a security hole in 4.4 only, by which a non-default app can gain write access to the Provider. It is detailed in my answer here, but it will not work in versions after KitKat.