When I dial *#06# on my phone the IMEI is displayed. Fine.
But when I use this kind of code :
String imei="*#06#";
Intent cintent= new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+Uri.encode(imei)));
startActivity(cintent);
(or ACTION_CALL instead of ACTION_DIAL)
... the USSD code is displayed correctly on the dialer (*#06#). But even if I click call the IMEI never shows up (error : "Connection problem or invalid MMI code").
I've been through other similar topics, but found no solution and can't figure how to make it work (my phone is running Android 4.4.4).
Note that I don't want to get the result of the USSD, just have it executed somehow.If it is possible...
Thanks for your help !
We have a different way to get IMEI of device . you can follow the link(possible duplicate).How can I get phone serial number (IMEI)
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();
And you should add the following permission into your Manifest.xml file:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Related
I found out that this code shared by some users doesn't work in my project. Has anyone a better solution ?
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
I'm getting an error on the getLine1Number() and on the mAppContext
Your code is for native Android (Java). Try the below code of Xamarin.Android (c#).
Code:
Android.Telephony.TelephonyManager tMgr = (Android.Telephony.TelephonyManager)this.GetSystemService(Android.Content.Context.TelephonyService);
string mPhoneNumber = tMgr.Line1Number;
Required Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
[Edited]
Sometimes Line1Number Property will return null if the number is unavailable, but it does not say when the number might be unavailable.
These are occured due to the reason that the phone number not register in your mobile, or according to some restrictions created by the device manufactures.
From Documentation :
Returns the phone number string for line 1, for example, the MSISDN for a GSM phone. Return null if it is unavailable.
So you have done everything right, but there is no phone number stored.
If you get null, you could display something to get the user to input the phone number on his/her own.
Did you add <uses-permission android:name="android.permission.READ_PHONE_STATE"/> to manifest?
It's not really perfect, but the only way (at least to my knowledge). Broader discussion is here Programmatically obtain the phone number of the Android phone
I have tried below code
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
I have tried in two device
Micromax Canvas-4 (Getting user phone number)
Htc desire 816G (Not able to get user phone number)
Please help me !!
Your help will be appreciated. Thanks in advance.
It is occurring because you need to set your own phone number on the device.
please have a look at the below link.
getLine1Number() Returns blank, not null
This will solve your problem.
I am trying to get IMEI of a phone by running this android code
String imei="*#06#";//Checking the Phone's IMEI.
Intent cintent= new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+Uri.encode(imei)));
startActivity(cintent);
I have set the permission as required
<uses-permission android:name="android.permission.CALL_PHONE"/>
But am getting this feedback on my phone "Connection problem or invalid MMI code".
Where did I go wrong? Thanks
A simple change of this line did the magic. From
Intent.ACTION_CALL
TO:
Intent.ACTION_DIAL
Thanks
I have used the standard code below to fetch the mobile number on my ANDROID 2.2 GSM device, BUT I ALWAYS GET AN EMPTY STRING. Is this feature supported on 2.2?
Any suggestion as to how I can fetch the mobile number on Android 2.2 device.
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
Go to Settings -> About phone -> Status -> see at My Phone Number field. If it says "Unknown" then it's not your issue, it's just the fact that some carriers don't pack the SIM cards with own number. On some devices user can edit own number, but it's not always supported by the firmware.
Read here
That should work, but make sure you're requesting the READ_PHONE_STATE permission in your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
I use the TelephonyManager to do this and you can access TelephonyManager s methods as given below
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = telephonyManager.getLine1Number();
The documentation for getLine1Number() says this method will return null if the number is "unavailable", but it does not say when the number might be available.
I have also given application permission to make this query by adding the following to your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
The number is always blank or null...
While I have also crosschecked calling and i'm able to call from that number(i.e inserted sim no)
I have gone through numerous documentations but nothing help me out to get the phone number from sim so as to be used in my app..
With Regards,
Arpit Garg
AFAIK, TelephonyManager.getLine1Number() is not reliable due to various constraints from operators.
There are some java reflection based hacks but varies from device to device thus making those hacks sort of useless [at least in terms of supported models]
But there is a legitimate lawful logic to find the number, if you really need so. Query all the SMS by sms provider and get the "To" number.
Extra benefits of this trick: 1. you can get all the line numbers if there is multi sim in the device.
Cons: 1. you will need SMS_READ permission [sorry for that]
2. You will get all the sim numbers ever used in the device. this problem can be minimized with some constraint logic e.g. time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case.
you should try this
TelephonyManager phoneManager = (TelephonyManager)
getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();