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();
subrussn90
  • 1,142
  • 1
  • 14
  • 27

1 Answers1

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);
}

}

Sushil
  • 248
  • 3
  • 9
  • Yes,i tried that...but im not able to start the service...THANX for the reply frnds.... – subrussn90 Dec 25 '11 at 16:32
  • 2
    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); } } – Sushil Dec 25 '11 at 17:13
  • 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? – subrussn90 Dec 25 '11 at 17:19
  • 2
    can you confirm that you have added manifest permission - . Because I have tested program on emulator & it's working – Sushil Dec 25 '11 at 17:25
  • 1
    `telephoneMgr.getLine1Number();` is a very unreliable method by the way. – Abdul Wasae Jul 17 '17 at 06:20