0

Hi friends i am using the following code to detect SIM change but not able to get the result. Please suggest me where i am making the mistake.

I am Using MainActivity.java class for Storing SIM msisdn. like this

public class MainActivity extends Activity {
String FILENAME = "old_file.txt";
int simstatus;
String msisdn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    simstatus = tManager.getSimState();
    if (simstatus != TelephonyManager.SIM_STATE_ABSENT) {
        System.out.println("--------SIM Present:" + simstatus);
        msisdn = tManager.getLine1Number();
        FileOutputStream fos;
        try {
            fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            fos.write(msisdn.getBytes());
            System.out.println("---------Data written to files is:"
                    + msisdn);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

}

And for receiving the Reboot event i am using SIMTestReceiver class like this.

public class SIMTestReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))  {
        Intent CompareSimServiceIntent = new Intent(context,TestDATAScreen.class);
        context.startService(CompareSimServiceIntent);
    }

}

And for comparing the old SIM and New SIM i am using TestDATAScreen service class like this.

public class TestDATAScreen extends Service {

String FILENAME = "old_file.txt";

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    try {
        FileInputStream fis = openFileInput(FILENAME);
        InputStreamReader in = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(in);
        String data = br.readLine();
        System.out.println("---Data Read From File is:" + data);
        String newsiminfo = tManager.getLine1Number();
        System.out.println("---New SIM no is:" + newsiminfo);
        if (data.equals(tManager.getLine1Number())) {
            System.out.println("------Old sim Present:");

            Toast.makeText(this, "Old SIM", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "New SIM", Toast.LENGTH_LONG).show();
            SmsManager smsMngr = SmsManager.getDefault();
            String destinationaddress = "8689908070";
            String scAddress = null;
            String text = "New Sim Is Inserted In Your Device";
            PendingIntent sentIntent = null;
            PendingIntent deliveryIntent = null;
            smsMngr.sendTextMessage(destinationaddress, scAddress, text,
                    sentIntent, deliveryIntent);
            System.out.println("-----SMS Send");
        }

    } catch (Exception e) {

    }
    return startId;

}
Mr Nice
  • 524
  • 8
  • 24

2 Answers2

1

You first save the sim and then compare the saved sim with the current sim. You never load the old sim's serialnumber.

Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54
  • Yes. But you overwrite the old serial with the new serial. Before you load the old serial. You should save the new serial after you loaded the old one. – Franziskus Karsunke Aug 13 '12 at 11:12
0

I found the solution. Actually comparing serial number using tManager.getSimSerialNumber() Solve my issue. AS There is no guaranteed result from tManager.getLine1Number()to this problem because the phone number is not physically stored on all SIM-cards, or broadcasted from the network to the phone.

Mr Nice
  • 524
  • 8
  • 24