无法检测到android中的SIM更改

时间:2012-08-13 11:02:52

标签: java android file

嗨朋友我正在使用以下代码来检测SIM更改但无法获得结果。 请建议我在哪里犯错误。

我正在使用MainActivity.java类来存储SIM msisdn。像这样

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

        }
    }

}

为了接收Reboot事件,我正在使用这样的SIMTestReceiver类。

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

}

为了比较旧SIM和新SIM,我正在使用像这样的TestDATAScreen服务类。

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;

}

2 个答案:

答案 0 :(得分:1)

首先保存SIM卡,然后将保存的SIM卡与当前的SIM卡进行比较。你永远不会加载旧的sim的序列号。

答案 1 :(得分:0)

我找到了解决方案。实际上使用tManager.getSimSerialNumber()比较序列号解决我的问题。 AS tManager.getLine1Number()无法保证此问题的结果,因为电话号码不是物理存储在所有SIM卡上,也不是从网络广播到手机。