简体   繁体   English

无法检测到Android中的SIM卡更改

[英]Not able to detect SIM change in android

Hi friends i am using the following code to detect SIM change but not able to get the result. 嗨,朋友们,我正在使用以下代码来检测SIM卡更改,但无法获得结果。 Please suggest me where i am making the mistake. 请告诉我我在哪里犯错。

I am Using MainActivity.java class for Storing SIM msisdn. 我正在使用MainActivity.java类存储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. 为了接收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);
    }

}

And for comparing the old SIM and New SIM i am using TestDATAScreen service class like this. 为了比较旧的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;

}

You first save the sim and then compare the saved sim with the current sim. 您首先保存sim,然后将保存的sim与当前sim进行比较。 You never load the old sim's serialnumber. 您永远不会加载旧版SIM卡的序列号。

I found the solution. 我找到了解决方案。 Actually comparing serial number using tManager.getSimSerialNumber() Solve my issue. 使用tManager.getSimSerialNumber()实际比较序列号解决了我的问题。 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. AS tManager.getLine1Number()无法解决此问题,因为电话号码不是物理存储在所有SIM卡上,也不是从网络广播到电话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM