In my android code, I am trying to detect incoming SMS messages. The code below was working since 2 years, but now it stopped working. What updates am I missing?
public class SmsListener extends BroadcastReceiver {
private String msgBody;
private SharedPreferences preferences;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Toast.makeText(context,"message received",Toast.LENGTH_SHORT).show();
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
MainActivity.handleMessage(msgBody);
}
Toast.makeText(context,"message is:"+msgBody,Toast.LENGTH_SHORT).show();
}catch(Exception e){
Log.d("Exception caught",e.getMessage());
}
}
}
}
In my Main Activity I am requesting the user permission, and using the SMS receiver as the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Updated///////////////////////////////////////////////////////////////////////////////////
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestSmsPermission();
else {
smsListener = new SmsListener();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsListener, intentFilter);
}
///////Updated///////////////////////////////////////////////////////////////////////////////////
}
private void requestSmsPermission() {
String permission = Manifest.permission.RECEIVE_SMS;
int grant = ContextCompat.checkSelfPermission(this, permission);
if ( grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
smsListener = new SmsListener();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsListener, intentFilter);
}
}
My Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zaidalmahmoud.expenseless">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".GraphActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".SmsListener"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity android:name=".DetailedExpenseActivity" />
</application>
</manifest>
When the SMS is received, my android application does not detect it, but it does not crash or show anything. Why? Thanks.
NOTE: I am not interested in catching SMS with verification code. I want my application to catch any incoming SMS.
The Android app needs SMS receive/read permission to retrieve SMS content.
Google has introduced SMS Retriever API, this API allows to retrieve the OTP without needing of the SMS permission in your application.
Add These Dependency for SMS Retriever API
implementation 'com.google.android.gms:play-services-base:16.0.1'
implementation 'com.google.android.gms:play-services-identity:16.0.0'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.google.android.gms:play-services-auth-api-phone:16.0.0'
Then craete an interface like below:
public interface OnNewMessageListener {
void onNewMessageReceived(String activationCode);
}
Then, create a broadCastReceiver to catch sms:
public class SmsBroadcastReceiver extends BroadcastReceiver {
OnNewMessageListener onNewMessageListener;
public SmsBroadcastReceiver() {
}
public SmsBroadcastReceiver(OnNewMessageListener onNewMessageListener) {
this.onNewMessageListener = onNewMessageListener;
}
#Override
public void onReceive(Context context, Intent intent) {
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
if (extras != null) {
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
if (status != null)
switch (status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
// Get SMS message contents
String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
// Extract one-time code from the message and complete verification
// by sending the code back to your server.
if (!TextUtils.isEmpty(message)) {
String activationCode = null;
Pattern p = Pattern.compile("your pattern like \\b\\d{4}\\b");
Matcher m = p.matcher(message);
if (m.find()) {
activationCode = (m.group(0)); // The matched substring
}
if (onNewMessageListener != null && !TextUtils.isEmpty(activationCode))
onNewMessageListener.onNewMessageReceived(activationCode);
}
break;
case CommonStatusCodes.TIMEOUT:
// Waiting for SMS timed out (5 minutes)
// Handle the error ...
break;
}
}
}
}
}
At your AndroidManifest declare broadcastReceiver:
<receiver
android:name=".SmsBroadcastReceiver"
android:exported="true"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
</intent-filter>
</receiver>
Inside your activity add these code:
private SmsBroadcastReceiver smsListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get an instance of SmsRetrieverClient, used to start listening for a matching
// SMS message.
SmsRetrieverClient client = SmsRetriever.getClient(Objects.requireNonNull(getContext()) /* context */);
// Starts SmsRetriever, which waits for ONE matching SMS message until timeout
// (5 minutes). The matching SMS message will be sent via a Broadcast Intent with
// action SmsRetriever#SMS_RETRIEVED_ACTION.
Task<Void> task = client.startSmsRetriever();
// Listen for success/failure of the start Task. If in a background thread, this
// can be made blocking using Tasks.await(task, [timeout]);
task.addOnSuccessListener(aVoid -> {
// Successfully started retriever, expect broadcast intent
// ...
});
task.addOnFailureListener(e -> {
// Failed to start retriever, inspect Exception for more details
// ...
});
OnNewMessageListener onNewMessageListener = activationCode -> {
if (!TextUtils.isEmpty(activationCode)) {
editText.setText(String.valueOf(activationCode));
}
};
smsListener = new SmsBroadcastReceiver(onNewMessageListener);
if (getContext() != null)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getContext().registerReceiver(smsListener, new IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION));
}
}
#Override
public void onStop() {
super.onStop();
try {
if (getContext() != null && smsListener != null) {
getContext().unregisterReceiver(smsListener);
smsListener = null;
}
} catch (Exception ignored) {
}
}
Your sms should be like this:
<#> Use 123456 as your verification code
FC+7qAH5AZu
Message must:
Be no longer than 140 bytes
Begin with the prefix <#>
Contain a one-time code that the client sends back to your server to complete the verification flow (see Generating a one-time code)
End with an 11-character hash string that identifies your app (see Computing your app’s hash string)
For more information see this link.
UPDATE
See this link.
EDIT
Change your activity to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
requestSmsPermission();
else {
smsListener = new SmsListener();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsListener, intentFilter);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
smsListener= new SmsListener();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsListener, intentFilter);
}
}
Update
Change your BroadcastReceiver to this:
public class SmsListener extends BroadcastReceiver {
private String msgBody;
private SharedPreferences preferences;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Toast.makeText(context, "message received", Toast.LENGTH_SHORT).show();
Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage smsMessage;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i], bundle.getString("format"));
else smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
msg_from = smsMessage.getDisplayOriginatingAddress();
msgBody = smsMessage.getMessageBody();
MainActivity.handleMessage(msgBody);
}
Toast.makeText(context, "message is:" + msgBody, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.d("Exception caught", e.getMessage());
}
}
}
}
Declare SMS Read and Receive permission in your android manifest file :
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
Then we need a SMS Receiver as follows:
class SMSReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
if (intent != null && intent.action != null && intent.action!!.equals("android.provider.Telephony.SMS_RECEIVED", ignoreCase = true)) {
val bundle = intent.extras
if (bundle != null) {
val sms = bundle.get(SMS_BUNDLE) as Array<Any>?
val smsMsg = StringBuilder()
var smsMessage: SmsMessage
if (sms != null) {
for (sm in sms) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val format = bundle.getString("format")
smsMessage = SmsMessage.createFromPdu(sm as ByteArray, format)
} else {
smsMessage = SmsMessage.createFromPdu(sm as ByteArray)
}
val msgBody = smsMessage.messageBody.toString()
val msgAddress = smsMessage.originatingAddress
smsMsg.append("SMS from : ").append(msgAddress).append("\n")
smsMsg.append(msgBody).append("\n")
}
sendBroadcast(smsMsg.toString())
}
}
}
}
private fun sendBroadcast(smsMSG: String) {
val broadcastIntent = Intent()
broadcastIntent.action = AppConstants.mBroadcastSMSUpdateAction
broadcastIntent.putExtra(AppConstants.message, smsMSG)
EventBus.getDefault().post(EventIntent(broadcastIntent))
}
companion object {
val SMS_BUNDLE = "pdus"
}
}
You can broadcast the received SMS information to your MainActivity.
Then you need to declare the SMS Broadcast receiver in your manifest file as follows:
<receiver
android:name=".SMSReceiver"
android:enabled="true">
<intent-filter android:priority="2147483647">
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
After that on application launch in MainActivity you need to check for SMS permissions by checking:
Manifest.permission.READ_SMS,
Manifest.permission.RECEIVE_SMS
You can use built in android permission module or RxPermission for that :
val rxPermissions = RxPermissions(this)
rxPermissions.request(
Manifest.permission.READ_SMS,
Manifest.permission.RECEIVE_SMS)
.subscribe(object : Observer<Boolean> {
override fun onNext(t: Boolean) {
if (t) {
} else {
Toast.makeText(activity, getString(R.string.permission_request_denied), Toast.LENGTH_LONG).show()
}
}
override fun onSubscribe(d: Disposable) {}
override fun onError(e: Throwable) {}
override fun onComplete() {}
})
In my application where the app receives a new sms message, I want the listview to update it it immediately with the new message shown in the listview. I tried using the example code of the broadcast receiver code but it didn't work, so I'm now trying to use a timer to update the listview every second and show the latest message received but this isn't working either.
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
inboxArrayAdapter.notifyDataSetChanged();
((BaseAdapter)inboxmessages.getAdapter()).notifyDataSetChanged();
handler.postDelayed(this, 1000 );
}
}, 1000);
return view;
Original attempt of using BroadcastReceiver to update listview when new SMS message recieved.
SmSBroadcastReceiver.java
public class SmsBroadcastReceiver extends BroadcastReceiver
{
public static final String SMS_BUNDLE = "pdus";
// https://javapapers.com/android/android-receive-sms-tutorial/
public void onReceive(Context context, Intent intent)
{
Bundle intentExtras = intent.getExtras();
if (intentExtras != null)
{
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; i++)
{
// Get sms message
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
// Get content and number
String smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
// Create display message
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
// Add it to the list
CommandsFragment inst = CommandsFragment.instance();
inst.updateList(smsMessageStr);
}
CommandsFragment inst = CommandsFragment.instance();
inst.updateList(smsMessageStr);
}
}
}
CommandsFragment.java
public static CommandsFragment instance()
{
return inst;
}
#Override
public void onStart() {
super.onStart();
inst = this;
}
public static CommandsFragment newInstance()
{
CommandsFragment fragment = new CommandsFragment();
return fragment;
}
public void updateList(final String smsMessage)
{
inboxArrayAdapter.insert(smsMessage, 0);
inboxArrayAdapter.notifyDataSetChanged();
}
Put the listview update code inside a method in the Activity where the listview belongs, and call that method from broadcast receiver's onReceive. It would definitely work. Also check the code for whether you are updating the list view properly.
public class SmsBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context paramContext, Intent intent) { // change the parameters depending upon your requirements
// do something here with the sms received if necessary
// then call the listview update method here
}
}
Also to add to the answer don't forget to have this below in the AndroidManifest.xml file
</activity>
<receiver android:name=".SmsBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
This above also fixed the solution for me. Thanks for your help guys.
I want to open my application when I get notification message "abc". I can do this with SMSReceiver but only get sms message. I want do this whatsapp message. sory for bad English.
#Override
public void onReceive(Context context, Intent intent) {
String hangiNumaradan = "";
String neYazmis = "";
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
hangiNumaradan += msgs[i].getOriginatingAddress();
neYazmis += msgs[i].getMessageBody().toString();
}
Toast.makeText(context, hangiNumaradan + " gelen mesaj " + neYazmis, Toast.LENGTH_LONG).show();
}
}
This code is smsreceiver and toast message.
After doing some investigation I realized that since Facebook bought Whatsapp, they closed all public Whatsapp API and right now it's impossible to create Whatsapp message receiver, BUT there is one thing that you can do.
Create AccessibilityService:
public class MyAccessibilityService extends AccessibilityService {
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
//here you can implement your reaction for incoming notification
//here you can find some tags that could be helpful for you
String helpful = event.getContentDescription();
//In code below you could also retrieve some helpful info from Notification
AccessibilityNodeInfo source = event.getSource();
if (source == null) {
return;
}
// Grab the parent of the view that fired the event.
AccessibilityNodeInfo rowNode = getListItemNodeInfo(source);
if (rowNode == null) {
return;
}
// Using this parent, get references to both child nodes, the label and the checkbox.
AccessibilityNodeInfo labelNode = rowNode.getChild(0);
if (labelNode == null) {
rowNode.recycle();
return;
}
AccessibilityNodeInfo completeNode = rowNode.getChild(1);
if (completeNode == null) {
rowNode.recycle();
return;
}
// Determine what the task is and whether or not it's complete, based on
// the text inside the label, and the state of the check-box.
if (rowNode.getChildCount() < 2 || !rowNode.getChild(1).isCheckable()) {
rowNode.recycle();
return;
}
CharSequence anotherInfoFromNotification = labelNode.getText();
}
#Override
public void onInterrupt() {
}
}
Create serviceconfig.xml file:
<accessibility-service
android:accessibilityEventTypes="typeViewClicked|typeViewFocused"
android:packageNames="com.whatsapp"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:settingsActivity="com.example.android.your.way.to.activity"
android:canRetrieveWindowContent="true"/>
Register service in manifest:
<service android:name=".MyAccessibilityService">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data android:name="android.accessibilityservice"
android:resource="#xml/serviceconfig" />
</service>
So to summ it up: you will be able to get listener to all incoming whatsapp messages, you have to try little bit more with code in onAccessibilityEvent method to retrive info from message.
I have some problem with passing throught my variable from Activity to the BroadcastReceiver...
Here is my code:
here is my Broadcast receiver code... I try to catch SMS from one phone number which I have got from my Activity...
public class SMSMonitor extends BroadcastReceiver
{
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public static String phone_number = "";
public static String msg_body = "";
public static final String SMS_EXTRA_NAME = "pdus";
#Override
public void onReceive(Context context, Intent intent)
{
String phone = intent.getExtras().getString("trusted_num");
if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0)
{
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i < pduArray.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
}
phone_number = messages[0].getDisplayOriginatingAddress();
msg_body = messages[0].getMessageBody();
System.out.println("Phone number: "+phone_number);
System.out.println("Phone entered: "+phone);
}
}
}
Here is my Activity code:
public class Settings extends Activity implements OnClickListener{
private Button btn_save;
private EditText txt_phone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//set Save button
btn_save = (Button)findViewById(R.id.btn_save);
txt_phone = (EditText)findViewById(R.id.et_phone);
btn_save.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_settings, menu);
return true;
}
#Override
public void onClick(View v)
{
if (v == btn_save)
{
try
{
String phone_num = txt_phone.getText().toString();
Intent i = new Intent(Settings.this, SMSMonitor.class);
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);
}
catch(Exception e)
{
System.out.println("Error: "+e.getLocalizedMessage());
}
}
}
}
In this code I have text field for entering the phone number, which I need to pass to the BroadcastReceiver with intent.putExtra() method, but in LogCat I see, that variable didnot pass:
07-25 18:43:57.382: I/System.out(14245): Phone number: +37129690449
07-25 18:43:57.382: I/System.out(14245): Phone entered: null
So what I am doing wrong here?
UPD
Maybe code is not correct, but it works for me...
public void onReceive(Context context, Intent intent)
{
phone = intent.getExtras().getString("trusted_num");//get trusted phone number from Settings screen
//receive SMS
if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0)
{
Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] messages = new SmsMessage[pduArray.length];
for (int i = 0; i < pduArray.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
}
phone_number = messages[0].getDisplayOriginatingAddress();
msg_body = messages[0].getMessageBody();
System.out.println("Phone number: "+phone_number);
}
//check if number is not null
if (phone != null && phone != "")
{
System.out.println("Phone entered: "+phone);
}
}
}
You can't pass an intent to a broadcast receiver. "There is no way for a BroadcastReceiver to see or capture Intents used with startActivity()"
https://developer.android.com/reference/android/content/BroadcastReceiver.html
I had a similar issue a while back and solved it by using a combination of IntentServices and Activities. You have to restructure your program to fit these guidlines
Well, there are some things not that don't match:
You're sending an intent with no action in the first place, but you're specifying Broadcastreceiver's class; don't do like that:
Intent i = new Intent(Settings.this, SMSMonitor.class);
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);
But try instead:
Intent i = new Intent("my_package_name.Some_general_constant");
i.putExtra("trusted_num", phone_num);
sendBroadcast(i);
Then, your BroadcastReceiver is supposed to know that it can also handle "Some_general_constant" action. For this reason, register an extra action in your Manifest file for your SMSMonitor:
<receiver android:name=".package_to_bla_bla.SMSMonitor">
<intent-filter>
<action android:name="my_package_name.Some_general_constant"/>
</intent-filter>
</receiver>
Then in your SMSMonitor you need to add an else if statement to handle this broadcast:
else if("my_package_name.Some_general_constant".equals(intent.getAction())
{
// get the data from intent and use it
}
Mobile number will be entered in an edittext by user on registration page in my Android application. How can I check that user entered his/her mobile number not other's ?
I've tried this :
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
And compare this variable with edittext's text. But mPhoneNumber returns NULL in my case. Is there any other options? How to solve this ?
Any help would be appreciable.
I have tried this : Check source code :
public class MainActivity extends Activity{
Button submit;
EditText contact;
String phNo;
ProgressDialog progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contact = (EditText)findViewById(R.id.mobileNumber);
submit = (Button) findViewById(R.id.button1);
submit.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
phNo = contact.getText().toString();
new CheckOwnMobileNumber().execute();
Toast.makeText(getApplicationContext(), phNo, Toast.LENGTH_LONG).show();
}
});
}
private class CheckOwnMobileNumber extends AsyncTask<String, Void, String>
{
#Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
if(progress.isShowing())
{
progress.dismiss();
// Check SMS Received or not after that open dialog date
/*if(SMSReceiver.str.equals(phNo))
{
Toast.makeText(getApplicationContext(), "Thanks for providing your number.", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "Provide your own mobile number please.", Toast.LENGTH_LONG).show();
return;
}*/
}
}
#Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
String msg = phNo;
try
{
sendSMS(phNo, msg);
}
catch(Exception ex)
{
Log.v("Exception :", ""+ex);
}
return null;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progress = ProgressDialog.show(MainActivity.this, "","Checking Mobile Number...");
progress.setIndeterminate(true);
progress.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
super.onPreExecute();
}
}
private void sendSMS(String phoneNumber, String message)
{
//PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
Receiver to listen SMS received or not ?
public class SMSReceiver extends BroadcastReceiver
{
private static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
Context mContext;
private Intent mIntent;
static String address, str = null;
// Retrieve SMS
public void onReceive(Context context, Intent intent) {
mContext = context;
mIntent = intent;
String action = intent.getAction();
if(action.equals(ACTION_SMS_RECEIVED))
{
SmsMessage[] msgs = getMessagesFromIntent(mIntent);
if (msgs != null)
{
for (int i = 0; i < msgs.length; i++)
{
address = msgs[i].getOriginatingAddress();
str = msgs[i].getMessageBody().toString();
}
}
// ---send a broadcast intent to update the SMS received in the
// activity---
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
public static SmsMessage[] getMessagesFromIntent(Intent intent)
{
Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
byte[][] pduObjs = new byte[messages.length][];
for (int i = 0; i < messages.length; i++)
{
pduObjs[i] = (byte[]) messages[i];
}
byte[][] pdus = new byte[pduObjs.length][];
int pduCount = pdus.length;
SmsMessage[] msgs = new SmsMessage[pduCount];
for (int i = 0; i < pduCount; i++)
{
pdus[i] = pduObjs[i];
msgs[i] = SmsMessage.createFromPdu(pdus[i]);
}
return msgs;
}
}
LOGCAT :
03-13 17:31:02.049: E/ActivityManager(161): ANR in com.example.test
03-13 17:31:02.049: E/ActivityManager(161): Reason: Broadcast of Intent { act=android.provider.Telephony.SMS_RECEIVED cmp=com.example.test/.SMSReceiver (has extras) }
03-13 17:31:02.049: E/ActivityManager(161): 54% 3732/com.example.test: 54% user + 0% kernel / faults: 21 minor
03-13 17:31:02.049: E/ActivityManager(161): 40% 3732/com.example.test: 40% user + 0% kernel / faults: 2 minor
03-13 17:31:30.699: I/ActivityManager(161): Killing com.example.test (pid=3732): user's request
03-13 17:31:30.799: I/ActivityManager(161): Process com.example.test (pid 3732) has died.
03-13 17:31:30.799: I/WindowManager(161): WIN DEATH: Window{40992f50 com.example.test/com.example.test.MainActivity paused=false}
03-13 17:31:30.819: E/InputDispatcher(161): channel '40818670 com.example.test/com.example.test.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8
03-13 17:31:30.819: E/InputDispatcher(161): channel '40818670 com.example.test/com.example.test.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
03-13 17:34:59.649: I/ActivityManager(161): Start proc com.example.test for broadcast com.example.test/.SMSReceiver: pid=4037 uid=10098 gids={}
Its not guaranteed that tMgr.getLine1Number(); will always return your SIM card's number. Because it depends on the availability of number in SIM card. Like in my case, my Tre-Sweden SIM card doesn't contain my phone number.
But if you put the SIM card into an old SonyEricsson or Nokia phone, then you would get an option to edit this number (on SIM). Once its done, the android device will recognize the number and will show you.
Besides, if you do get your phone number through the code, then the best way to compare two numbers is:
boolean isSame = PhoneNumberUtils.compare(num1, num2);
Alternatively, you may implement some sort of pin-code verification logic (like Viber, WhatsApp or other application does) in which you ask the user to enter their phone number during registration. Later, that phone number is sent to the server and a pin-code is generated against that number which is sent to the user via SMS. Finally, the user has to enter that pin-code (received in SMS) to complete the registration.
Or
Simply send an SMS from user's device (with a consent) to your server/device and get to know their phone number.
Getting the phone number using getLine1Number() is not secure nor certain.
It is generally accepted because this whole "getting the phone number" is clash of multiple issues such as user's privacy, carrier's branding, and even the vendor's.
Anyway, unlike ios, android's android.provider.Telephony.SMS_RECEIVED makes the whole process very convenient and seemless to the user: You get to capture the sms and read it without any need of the user's intervention.
What is one way of doing it?
On your server, upon receiving the request to verify a phone number, you should generate a secret code, tokenSent, and send it to the app. Now, your server should send this code by sms to the specified phone number. The app by now should have a registered receiver listening for the android.provider.Telephony.SMS_RECEIVED intent. Once received, the app verifies that the tokenSent is identical to what it received from the server. At this point, phone registration is done and the server can be notified.
What could go wrong?
Generally, such apps are usually paid apps and it is not the user's good to attempt anything. Still, the user might enter a wrong number which he right now has. Then upon receiving the sms, he could forward it to the mobile where the app is registering. The app will then receive the tokenSent and wrongly verify the phone number.
How can we tackle this?
The feasibility of the solution depends on whether the sms provider allows your server to know the sender's phone number. This is probably (AFAIK) not gonna happen but if it does then you're in luck. That way, the app can, upon receiving the tokenSent, send it back to the server along with the sender of the sms. The server then can verify that this is the sms that was originated from your service provider.
Any more feasible solution? (If I am really paranoid)
In this case, the best solution, I believe, would be to request a tokenSent from your server. The server saves a generated tokenSent along with the phone number entered and sends this token to the app. The app notifies the user that registration will cost him 1 sms. Once the user accepts, you can easily send an sms in the background containing this tokenSent to a certain service. The server, once receives this tokenSent verifies the user using the token and the sender of the sms. Of course, this may seem a bit harassing and infringing to the user but it is the most secure way especially for such a paranoid (reading this part).
Formalities :P
Add Permissions in Manifest
<uses-permission android:name="android.permission.RECEIVE_SMS">
Register the receiver (Do this just before you send the sms to the phone)
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getExtras() != null)
{
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
SmsMessage[] msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
String from = msgs[i].getOriginatingAddress();
String body = msgs[i].getMessageBody().toString();
//here is the body
//...
unregisterReceiver(this); //If you are done with verification
}
}
}
}, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
I solved it my self. Here is my working code.
MainActivity Class :
public class MainActivity extends Activity
{
Button submit;
EditText contact;
static String phNo;
ProgressDialog progress;
static Boolean wasMyOwnNumber;
static Boolean workDone;
final static int SMS_ROUNDTRIP_TIMOUT = 30000;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contact = (EditText)findViewById(R.id.mobileNumber);
submit = (Button) findViewById(R.id.button1);
wasMyOwnNumber = false;
workDone = false;
submit.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
phNo = contact.getText().toString();
new CheckOwnMobileNumber().execute();
}
});
}
private class CheckOwnMobileNumber extends AsyncTask<String, Void, String>
{
#Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
if(progress.isShowing())
{
progress.dismiss();
if(wasMyOwnNumber)
{
Toast.makeText(getApplicationContext(), "Number matched.", Toast.LENGTH_LONG).show();
wasMyOwnNumber = false;
workDone = false;
}
else
{
Toast.makeText(getApplicationContext(), "Wrong number.", Toast.LENGTH_LONG).show();
wasMyOwnNumber = false;
workDone = false;
return;
}
}
super.onPostExecute(result);
}
#Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
String msg = phNo;
try
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phNo, null, msg, null, null);
timeout();
}
catch(Exception ex)
{
Log.v("Exception :", ""+ex);
}
return null;
}
#Override
protected void onPreExecute()
{
// TODO Auto-generated method stub
progress = ProgressDialog.show(MainActivity.this, "","Checking Mobile Number...");
progress.setIndeterminate(true);
progress.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
super.onPreExecute();
}
}
private boolean timeout()
{
int waited = 0;
while (waited < SMS_ROUNDTRIP_TIMOUT)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
waited += 100;
if(phoneNumberConfirmationReceived())
{
waited=SMS_ROUNDTRIP_TIMOUT;
workDone = true;
}
}
/*Log.v("MainActivity:timeout2: Waited: " , ""+waited);
Log.v("MainActivity:timeout2:Comparision: ", ""+ phoneNumberConfirmationReceived());
Log.v("MainActivity:timeout2: WorkDone value after wait complete : ", ""+workDone);*/
return workDone;
}
private boolean phoneNumberConfirmationReceived()
{
if(wasMyOwnNumber)
{
workDone = true;
}
return workDone;
}
}
SMSReceiver Code :
public class SMSReceiver extends BroadcastReceiver
{
private static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
Context mContext;
private Intent mIntent;
static String address, str = null;
boolean isSame;
// Retrieve SMS
public void onReceive(Context context, Intent intent)
{
mContext = context;
mIntent = intent;
String action = intent.getAction();
if(action.equals(ACTION_SMS_RECEIVED))
{
SmsMessage[] msgs = getMessagesFromIntent(mIntent);
if (msgs != null)
{
for (int i = 0; i < msgs.length; i++)
{
address = msgs[i].getOriginatingAddress();
str = msgs[i].getMessageBody().toString();
}
}
Log.v("Originating Address : Sender :", ""+address);
Log.v("Message from sender :", ""+str);
isSame = PhoneNumberUtils.compare(str, MainActivity.phNo);
Log.v("Comparison :", "Yes this true. "+isSame);
if(isSame)
{
MainActivity.wasMyOwnNumber = isSame;
MainActivity.workDone=true;
}
// ---send a broadcast intent to update the SMS received in the
// activity---
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
public static SmsMessage[] getMessagesFromIntent(Intent intent)
{
Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
byte[][] pduObjs = new byte[messages.length][];
for (int i = 0; i < messages.length; i++)
{
pduObjs[i] = (byte[]) messages[i];
}
byte[][] pdus = new byte[pduObjs.length][];
int pduCount = pdus.length;
SmsMessage[] msgs = new SmsMessage[pduCount];
for (int i = 0; i < pduCount; i++)
{
pdus[i] = pduObjs[i];
msgs[i] = SmsMessage.createFromPdu(pdus[i]);
}
return msgs;
}
}
No ANR found.
public class MainActivity extends Activity{
Button submit;
EditText contact;
String phNo;
ProgressDialog progress;
Boolean wasMyOwnNumber = false;
Boolean workDone = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contact = (EditText)findViewById(R.id.mobileNumber);
submit = (Button) findViewById(R.id.button1);
submit.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
phNo = contact.getText().toString();
new CheckOwnMobileNumber().execute();
Toast.makeText(getApplicationContext(), phNo, Toast.LENGTH_LONG).show();
}
});
}
private class CheckOwnMobileNumber extends AsyncTask<String, Void, String>
{
#Override
protected void onPostExecute(String result)
{
// TODO Auto-generated method stub
if(progress.isShowing())
{
progress.dismiss();
// Check SMS Received or not after that open dialog date
/*if(SMSReceiver.str.equals(phNo))
{
Toast.makeText(getApplicationContext(), "Thanks for providing your number.", Toast.LENGTH_LONG).show();
wasMyOwnNumber=true;workDone=true;
}
else
{
Toast.makeText(getApplicationContext(), "Provide your own mobile number please.", Toast.LENGTH_LONG).show();
wasMyOwnNumber=false;workDone=true;
return;
}*/
}
}
#Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
String msg = phNo;
try
{
sendSMS(phNo, msg);
int count=0;
while(!workDone)
{count++;}
}
catch(Exception ex)
{
Log.v("Exception :", ""+ex);
}
return null;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
progress = ProgressDialog.show(MainActivity.this, "","Checking Mobile Number...");
progress.setIndeterminate(true);
progress.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
super.onPreExecute();
}
}
private void sendSMS(String phoneNumber, String message)
{
//PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
public class SMSReceiver extends BroadcastReceiver
{
private static final String ACTION_SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
Context mContext;
private Intent mIntent;
static String address, str = null;
// Retrieve SMS
public void onReceive(Context context, Intent intent) {
mContext = context;
mIntent = intent;
String action = intent.getAction();
if(action.equals(ACTION_SMS_RECEIVED))
{
SmsMessage[] msgs = getMessagesFromIntent(mIntent);
if (msgs != null)
{
for (int i = 0; i < msgs.length; i++)
{
address = msgs[i].getOriginatingAddress();
str = msgs[i].getMessageBody().toString();
}
}
// ---send a broadcast intent to update the SMS received in the
// activity---
workDone=true;
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("SMS_RECEIVED_ACTION");
broadcastIntent.putExtra("sms", str);
context.sendBroadcast(broadcastIntent);
}
}
public static SmsMessage[] getMessagesFromIntent(Intent intent)
{
Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
byte[][] pduObjs = new byte[messages.length][];
for (int i = 0; i < messages.length; i++)
{
pduObjs[i] = (byte[]) messages[i];
}
byte[][] pdus = new byte[pduObjs.length][];
int pduCount = pdus.length;
SmsMessage[] msgs = new SmsMessage[pduCount];
for (int i = 0; i < pduCount; i++)
{
pdus[i] = pduObjs[i];
msgs[i] = SmsMessage.createFromPdu(pdus[i]);
}
return msgs;
}
}
}
Just want to add a bit here to above explanations in the above answers. Which will save time for others as well.
In my case this method didn't returned any mobile number, an empty string was returned. It was due to the case that I had ported my number on the new sim. So if I go into the Settings>About Phone>Status>My Phone Number it shows me "Unknown".
This is probably because you have ported the number from one network to other.
If you are not able to check the number from API Then:
One way of doing that is you generate a text message to the Number and send a Random Generated no to the Mobile Number. You will have to ask the user to enter this Random generated number into your Application. Once it is entered in the application then you can send it onto the server to check whether the number passed in the text is correct or not (Which you have already saved on server against that mobile number).
I hope this makes sense.