What will happen when getSimCountryIso() is called on Android Tablets? - java

In my Android app, in order to determine certain features, I'm checking a device's ISO country code using getSimCountryIso() and getNetworkCountryIso().
Tested this on my real mobile devices and emulators and all returned certain values without any errors.
One of them is my daily driver phone which has a SIM card in it, but the others have no SIM cards. So at first, I thought both getSimCountryIso() and getNetworkCountryIso() always return some values, but now I'm in doubt because all my real devices used to have SIM cards once and emulators may differ from real ones.
I wonder what will happen when those are called on real Tablets which NEVER HAD SIM cards in them and have only WiFi connections. Can I always expect a certain value? Or an exception or null?

I thought you would get a NullPointerException, but having tested it on a tablet - Pixel C, running Nougat - I actually got back empty Strings for both TelephonyManager.getSimCountryIso() and TelephonyManager.getNetworkCountryIso().
I had assumed the result would be an NPE because calling PackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) on a tablet will return false. That was enough to tell me that you shouldn't be trying to get SIM or network country codes this way on a non-phone device. However, if you try to retrieve the TelephonyManager using Context.getSystemService() on such a device anyway, you still get a non-null TelephonyManager - it just won't be helpful in providing a sim or network country.
In my opinion, if you are dealing with a device that does not have the Telephony system feature, you ought to consider using GPS location, or the user-set locale if you really don't want to encroach on location permissions and can tolerate the user-set locale not being where the user necessarily is.

Related

Android Development CellID in LTE network

I try to implement a function into my Android Application that finds out the cell id of the cell I'm in in a LTE network.
I recently read a lot about using TelephonyManager.getAllCellInfo() and then filter for CellInfoLte, but also that it's not implemented on every device.
So in my case calling telephonymaganger.getAllCellInfo() returns null.
I'm testing with a Samsung phone, what seems not to be the best case.
Is there any way to find out the Cell ID or is it just not accessible yet?
I had same problem a few months ago when using getNeigbouringCells() and finally I found this:
The cellid implementation varies from mobile device to mobile device
since these features are considered to be optional. for example:
Samsung (all devices): getNeigbouringCells () is not supported at all
and always returns an empty list.
Please take a look at this for more information:
http://wiki.opencellid.org/wiki/Android_library
You can check it with another phone but not Samsung phones. You can find so many reports about other vendors that have the same problem.

Alternative method of getting Sim number if getLine1Number() returns null

I have written an app which sends a message to a number if the sim card of the phone is changed in other words if the phone number of the phone is changed. The code is working fine on android emulator but when I run this app on my phone, getLine1Number() of TelephonyManager class is returning null string.
Is there any other way to check if the sim card of the phone is changed?
Regards
You can use getSimOperatorName() concat to getSimSerialNumber() or getSimSerialNumber() only as phone number alternative.
Is more ease to detect sim changes in this way.

Device Tokens are different from one device to another device

Hi i am little bit confusion about Device Token so can any one guide me. I am using following code for getting DeviceToken.
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)dt
{
}
The above code is working fine and it is showing DeviceToken data ,that is 64 length data.
My doubt is Device Token is different from one device to another device.
If once we got DeviceToken using one device that same DeviceToken can use for another Device.
Of course the device token is different for each device. It identifies a unique device. It's like a phone number (actually it's even more unique than a phone number, since multiple phones can have the same phone number). If it was the same, how would the Apple Push Notifications server know to which device to send your notification?

Since when is the phone charging/discharging

I wanted to learn more about the Android Services / Broadcasts, so I started a simple project, to create a battery monitoring app. It turned out pretty good, I'm using it for a few days now, but I want to add a new function: to show since when is the phone charging/discharging.
First I thought that I would create two static fields in my BoradcastReciever extension class, where I get and publish the data about the battery, one for the actual state (charging/discharging), and one for the time, when the change in state happened. This way, I could just subtract from the current time the last change, and know exactly since when is the phone charging/discharging.
But there is a problem with this solution: It won't show the correct data at first, when a user starts the app. I wouldn't make a big deal of it, but I saw that Android tracks this data somewhere, because inside my phone settings I found this information, so why take the hard way.
So my question is: is there an easy way to get from the Android system the date/time (no matter what format) of the last charging state change?
I looked at the BatteryManager reference but there are no constants named after what I seek, and which I could use, to get the information from the Intent of my receiver.
The Android OS tracks the connect/disconnect of a power source, but does not make this data accessible to apps. You have to record this all yourself, using intent filters.
The two intent filters to use are android.intent.action.ACTION_POWER_CONNECTED and android.intent.action.ACTION_POWER_DISCONNECTED; with these, you can monitor when the power source is connected and disconnected.
You can find information about this process explained incredibly clearly here. Another blog describing the process can be found here.

Changing Android Device name in code

I am trying to change the name of the Android Device that my program that is currently running on because the name of the device will contain information that is relevant when it communicates with other phones. The name of the phone will be constantly changed as phone scans for other phones and calculates information. Any ideas on how to change the name of the phone within the java code? I can't image it being more than a few lines of code, but I can't find anything.
Thanks in advance.
It's quite easy, get an instance of the bluetooth adaptor (since the only name you can set is the bluetooth name I think) that refers to the local device and call setName("newName"); on it.
BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter();
myDevice.setName("new name");
Quoting the docs:
Valid Bluetooth names are a maximum of 248 bytes using UTF-8 encoding, although many remote devices can only display the first 40 characters, and some may be limited to just 20.
So be careful with what you set as the device name. Oh, on another note, you can't change the name if the device bluetooth is off. So the actual code after checking it would be something on the lines of the following:
BluetoothAdapter myDevice = BluetoothAdapter.getDefaultAdapter();
if(myDevice.getState() == BluetoothAdapter.STATE_ON){
myDevice.setName("new name");
}
Important to note:
If you are going to test this on an emulator, beware that there are not bluetooth capabilities on the emulators and therefor the getDefaultAdapter() method returns null, resulting in a NullPointerException :)

Categories

Resources