How to get IMEI in Android using code - android

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

Related

Android TelephonyManager doesn't work with android studio simulator?

I am using this simple code to retreive some phone device data, but for some reason any time I open the app on the emulator, it pops up a message saying "Unfortunately *APP NAME* has closed" and then it shuts down the app.
this is the code I am using:
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String IMEINumber=tm.getDeviceId();
I wrote that code just to test the Telephonymanager methods but nothing works except for:
"getPhoneType();"
any ideas what's the problem? maybe it is because I am runing it on my android studio emulator?
the problem it says: getDeviceId: Neither user 10058 nor current process has android.permission.READ_PHONE_STATE though I added a permission in the manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
(above the application part)
thank you for your help

Getting the number of the phone Xamarin.Android?

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

Fail to display IMEI using code snippet

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"/>

Intent.ACTION_CALL - "Mobile network not available"

When I test my application with the emulator I get the following message: "Mobile network not available". Ofcourse I don't expect it to actually call from the emulator, but I want some sort of confirmation that it works.
In my application I use an intent like:
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + Uri.encode(input.getText().toString())));
context.startActivity(intent);
I also implement this <uses-permission android:name="android.permission.CALL_PHONE"/> in the manifest-file.
Why is this?
EDIT: Seems like this only was a problem when I used GenyMotion. With the regular simulator, the call simulation worked.
You have to check and update if it works. This is my assumption which may work since I have not tried it by myself.
Open another emulator and note down its port number and use something like this
callIntent.setData(Uri.parse("tel:5554")); // assuming the second emulator port number is 5554
I am assuming this because, there is no sim card inserted in the emulator so you cannot call any real life phone number. But it is actually possible to dial one emulator from another using its port number using built in dialer app.

getting udid of android emulator

I want to get udid of android emulator..How can i get it.Has anyone implemented it before?
Usually you can use TelephonyManager.getDeviceId() to get udid of a device, but if you are using Android emulator, it will return null. Docs here
Update:
You need to require READ_PHONE_STATE permission in your AndroidManifest.xml, i.e., by adding following line in the file:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Categories

Resources