Android SIM change
Asked Answered
M

1

6

Is it possible to detect SIM number using TelephonyManager in android at boot startup ,using Service at bootup...

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String ss=tm.getSimSerialNumber();
Miticide answered 25/12, 2011 at 13:20 Comment(0)
R
11

You need to register a broadcast receiver for the boot completion action i.e android.intent.action.BOOT_COMPLETED

in onReceive of this receiver you can start your service get SIM number with below code lines

  TelephonyManager telephoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  String phoneNumber = telephoneMgr.getLine1Number();

Also need to have permission for reading phone number as READ_PHONE_STATE in manifest file.

you can start service from broadcast receiver as -

 public class BootListener extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    Intent intent = new Intent(context,Myservice.class);
    context.startService(intent);
}

}

Roomful answered 25/12, 2011 at 15:28 Comment(5)
Yes,i tried that...but im not able to start the service...THANX for the reply frnds....Miticide
you can start service from broadcast receiver as- public class BootListener extends BroadcastReceiver { @Override public void onReceive(Context context, Intent arg1) { Intent intent = new Intent(context,Myservice.class); context.startService(intent); } }Roomful
One step i forwarded...as said,i was able to start the serice at startup...but when i get the simserial number,the program crashes...Any idea for this problem?Miticide
can you confirm that you have added manifest permission - <uses-permission android:name="android.permission.READ_PHONE_STATE"/>. Because I have tested program on emulator & it's workingRoomful
telephoneMgr.getLine1Number(); is a very unreliable method by the way.Fellowship

© 2022 - 2024 — McMap. All rights reserved.