Verifying the phone number - android

In my application first thing the user should do is the registration. For that i need the phone no. So the user will give their 10 digit number. Now i need to verfy the user inputed the same number which holds by the phone. How to do that ? i tried .getLine1Number() but it is returing nothing.
After pressing the register button i am calling a url which will send a verification code to the phone number was inputed by the user. Till that i am showing a alert dialog. After receiving the message only i am dismissing the dialoge box.
So the problem happens when the user give a valid number which may not their number.
PS: I need to get the response through message only.

Only few service providers provide phone number via getLine1Number() method. Instead you can get a unique number for each sim using getSimSerialNumber() method.

Did you set permissions in Manifest READ_PHONE_STATE ?
Method still can return null
Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.

Related

Not getting user dialed number in Android 9.0 Even i have declared both Read call log and REad Phone State permission at Runtime as well

In my app I am trying to intercept all outgoing calls starting with 00 string. I used Telephony Manager for the same to get outgoing number and then intercept it. All code was working properly before Android 9.0. But in Android Pie once i made call then my app crashed because i am not getting User Dialed number in Receiver.
"Without the READ_CALL_LOG permission, however, the phone number field
that's provided in PHONE_STATE_CHANGED broadcasts and through
PhoneStateListener is empty."
So you have to ensure the user grants permission for reading phone number before you try to access any dialed number.
Also you have to keep a check and a backup plan in case user refuses permission. Your app should handle such case well so as to avoid any null pointers.

How to get default sim card number android programmatically

To send sms programmatically I am using,
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(toNumber, null, text, PendingIntent.getBroadcast(
this, 0, new Intent(SMS_SENT_ACTION), 0), null);
I have coded for api >=22 like,
SubscriptionManager subscriptionManager=(SubscriptionManager)getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> subscriptionInfoList=subscriptionManager.getActiveSubscriptionInfoList();
if(subscriptionInfoList!=null && subscriptionInfoList.size()>0){
for(SubscriptionInfo info:subscriptionInfoList){
String mobileNo=info.getNumber();
}
}
using subscription manager I am not getting sim 2 card number using info.getNumber();. It returns empty for Samsung j5 device.
Another important thing is, using SubscriptionManager I am getting line 1 number correctly but empty for line 2. But at the same time when I am trying to send sms using SmsManager.getDefault() it sends sms from line 2. In that device line 2 sim card is set as default.
But based on operators available in a dual sim device toNumber (the number to send sms) will be changed. Because of this I need to know operator name or sim card number of default sim of the device set by user to the number SmsManager.getDefault(). How can I get it know?
This is how you should get it (link here):
Get the SmsManager associated with the default subscription id. The
instance will always be associated with the default subscription id,
even if the default subscription id changes.
Note: For devices that support multiple active subscriptions at a
time, SmsManager will track the subscription set by the user as the
default SMS subscription. If the user has not set a default,
SmsManager may start an activity to kick off a subscription
disambiguation dialog. Most operations will not complete until the
user has chosen the subscription that will be associated with the
operation. If the user cancels the dialog without choosing a
subscription, one of the following will happen, depending on the
target SDK version of the application. For compatibility purposes, if
the target SDK level is <= 28, telephony will still send the SMS over
the first available subscription. If the target SDK level is > 28, the
operation will fail to complete.
Note: If this method is used to perform an operation on a device that
has multiple active subscriptions, the user has not set a default SMS
subscription, and the operation is being performed while the
application is not in the foreground, the SMS disambiguation dialog
will not be shown. The result of the operation will conclude as if the
user cancelled the disambiguation dialog and the operation will finish
as outlined above, depending on the target SDK version of the calling
application. It is safer to use getSmsManagerForSubscriptionId(int) if
the application will perform the operation while in the background
because this can cause unpredictable results, such as the operation
being sent over the wrong subscription or failing completely,
depending on the user's default SMS subscription setting.
So, the code would be:
val subscriptionManager = applicationContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
val defaultSubscriptionId = SmsManager.getDefaultSmsSubscriptionId()
val smsManager = SmsManager.getSmsManagerForSubscriptionId(defaultSubscriptionId)?:SmsManager.getDefault()
var defaultSubscriptionInfo: SubscriptionInfo? = subscriptionManager.getActiveSubscriptionInfo(defaultSubscriptionId)
To sum everything up, it seems there may be a bug in the Samsung J5 implementation of the dual SIM on Android 23 (resulting in a blank phone number in the SIM settings).

Getting userNo of a caller or calling entity

I am using sinch services in my android app and using mobile number as userID now. I want to know Suppose I am inside some activity and I want to get the number of other person he can be a caller or receiver. So can we get that number in
"As we have given to the sinch"
not in Return type "Call" but it should be same as we have given to Sinch like this "8888888888" so I should get this in my activity
Purpose : Same Feedback form for both receiver and caller will be created as soon as the call ends. So caller will rate receiver and vice versa. So I have to give the other person's number to the api to update rating.
OK I have Solved my own problem by using call.getRemoteUserId(); in callscreenactivity

How to check if user already logged in some other device android?

Hi I have requirement where i need to check if user already logged in some other device.
Sceario 1: If user logged in, I am making service call to change from 0 to 1. If user log out, i am making service call to 0. This works perfectly.
But problem is if user uninstall app after sign in, without log out, how to manage this scenario? As it changed to 1 and in this case
Scenario 2: I got suggestion that, if user un-install the app without log out or you're not log-in in app till 1 day you should change user's status to logout.but in this case, if user log in one device and uninstall app immediately within few hours(say may be because of low ram), once again user install app means, in this case the above scenario fails too.
I used device id from gcm to use, but device id also not an uinque one, if uninstalled app in same device and logged in, device id too will vary.
Please suggest me how to resolve this issue.
Thanks.
To check if user uninstalled the app and again installed and logged in, you can user unique id of android device which is :
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
for more details see here
scenario 1 is the simplest, but a bit edited :
instead of just make a "ping" to your service on login / logout (that may cause some problem if you miss logout for any reason, as uninstall app, network issue, etc), simpler is to have timed ping (let's say every 30 sec, your app send a ping)
on server side, at ping receipt, start a timer and keep some safety margin for latency. let's say, if no other ping came in 45 sec, you log out automatically
you can adjust duration or underlying mechanism but main idea is there
You can use unique device id that is associated with the device i.e. IMEI number.
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = telephonyManager.getDeviceId();
This device id is not reset and remains the same for a device.
for more details see here

Determining what # is being dialed on Android

I know that with the telephony manager listen, you can listen for 3 different states. Iknow that CALL_STATE_OFFHOOK indicates that there is at least one call that is dialing, active, etc. My question is- with the telephony manager, is there a way to determine what number the phone is off hook with? I thought getLine1Number() might return that phone number that is being dialed, but it is not what I expect. I am working with 2 emulators, and added a log line so that I could see what that method is doing. When dialing another emulator, I expected getLine1Number() to return 5554, but it was 15555218135. Perhaps there is another method I should be using instead? Do I need to be into the source code to get the information I want?
Clearly, getLine1Number() is returning the phone's number. I currently have a work around of having the user use the program to initiate a call. They enter the number to dial in a text box and that way I can capture the number.

Categories

Resources