使用codename1从SIM获取IMSI

时间:2014-07-20 08:50:11

标签: codenameone

我需要获得IMSI(国际 移动子用户身份)使用codename1存储在SIM卡中。同样在双卡或三卡手机的情况下,我需要为每个SIM卡获取IMSI。拜托,我怎么得到它?

2 个答案:

答案 0 :(得分:0)

Display.getMsisdn()适用于某些设备,但大多数设备不允许访问该信息。有关更多信息,您可以使用本机界面,如果您可以这样访问它。

答案 1 :(得分:0)

获取双Sim设备的IMSI的另一种方法:

试试这个..它为我工作。想法是为iphonesubinfo功能#3调用服务。您将获得输出作为宗地值,这就是为什么我使用getNumberFromParcel来提取数字。

import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Apipas on 6/4/15.
 */
public class SimUtil {

    public static String getIMSI_1() {
        String imsiParcel = runCommand("service call iphonesubinfo 3");
        String imsi = getNumberFromParcel(imsiParcel);
        Log.d("apipas", "IMSI_1:" + imsi);
        return imsi;
    }

    public static String getIMSI_2() {
        String imsiParcel = runCommand("service call iphonesubinfo2 3");
        String imsi = getNumberFromParcel(imsiParcel);
        Log.d("apipas", "IMSI_2:" + imsi);
        return imsi;
    }


    public static String runCommand(String src) {
        try {
            Process process = Runtime.getRuntime().exec(src);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[2048];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();

            // Waits for the command to finish.
            process.waitFor();

            return output.toString();
        } catch (IOException e) {
            Log.e("apipas", "IOException:" + e.getMessage());
            return null;

        } catch (InterruptedException e) {
            Log.e("apipas", "InterruptedException:" + e.getMessage());
            return null;
        }
    }


    public static String getNumberFromParcel(String str) {
        String res = "";
        if (str != null && str.length() > 0) {
            String lines[] = str.split("\n");
            for (String line : lines) {
                if (line == null || line.length() == 0)
                    continue;
                String content[] = line.split("'");
                if (content.length > 1) {
                    res += content[1].replace(".", "");
                }
            }
        } else return "NA";
        return res;
    }

}

然后调用这些静态方法,如:

String imsi1 = SimUtil.getIMSI_1();
String imsi2 = SimUtil.getIMSI_2();

您需要设置此权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>