问题
I am making an application and I want to retrieve the device phone number and send that on the server. But I am testing this application on android emulator. Can anybody please tell me how to set or get the phone number in emulator and actual device.
Thanks.
回答1:
We can get the phone number in emulator if we use the Telephony manager
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String phone = tm.getLine1Number();
Toast toast = Toast.makeText(getApplicationContext(), phone, Toast.LENGTH_SHORT);
toast.show();
On making the toast of the phone number we get the phone number like..
15555215554
It also need the android.permission.READ_PHONE_STATE permission. So Emulator is also having its own phone number.
回答2:
In DDMS perspective You will see Emulator Control tab you can emulate the call from there. If you don't see Emulator Control there . Use Window> Show /View > Emulator Control to make the tab appear.
To accesss device phone number in your code use telephonymanager.getLine1Number(); You need to set READ_PHONE_STATE permission in manifest file for that.
回答3:
you cannot call the emulator from your device. But you can make calls to two emulators. For details on how to ? you can go to DDMS perspective then Emulator Control tab you can make the call from there to other emulator. http://developer.android.com/tools/devices/emulator.html
来源:https://stackoverflow.com/questions/11599346/send-phone-number-through-emulator