0

嗨朋友我正在使用以下代码来检测 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;

}
4

2 回答 2

1

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

于 2012-08-13T11:08:01.423 回答
0

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

于 2012-08-16T12:00:01.677 回答