0

服务等级

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartActivityAtBoot extends BroadcastReceiver{

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

比较 SIM 卡 IMSI 类

public class CompareIMSI extends Service{

Context context;
TelephonyManager operator;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
    compareSIM();
}

@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}

public void compareSIM(){

    final String STORAGE = "Storage";
    SharedPreferences unique = getSharedPreferences(STORAGE, 0);
    final String storedIMSI = unique.getString("simIMSI", "");
    final String currentIMSI = getSubscriberId().toString();

    if (!storedIMSI.equals(currentIMSI)){
        Intent i = new Intent(CompareIMSI.this, ScreenLockActivity.class);
        startActivity(i);
    }
}

public String getSubscriberId(){

    String IMSI = null;
    String serviceName = Context.TELEPHONY_SERVICE;
    TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
    int deviceType = m_telephonyManager.getPhoneType();
    switch (deviceType) {
        case (TelephonyManager.PHONE_TYPE_GSM):
            break;
        case (TelephonyManager.PHONE_TYPE_CDMA):
            break;
        case (TelephonyManager.PHONE_TYPE_NONE):
            break;
        default:
            break;
     }
     IMSI = m_telephonyManager.getDeviceId();
     return IMSI;
}
}    

我希望应用程序能够在启动时将存储的 SIM 卡 IMSI 与当前插入的 IMSI 进行比较,如果 IMSI 不同,那么应用程序将在启动后将用户带到另一个活动......我的编码有什么问题?

4

2 回答 2

0

是的,getSubscriberId()是正确的方法,顺便说一句,当您收到 boot_completed 广播时,“imsi”通常不可用。

于 2013-03-22T03:27:43.737 回答
0

您不是在比较 IMSI,而是在比较 IMEI(这是手机的 ID,它永远不会改变)。

要获得您需要的 IMSI:

IMSI = m_telephonyManager.getSubscriberId();
于 2012-05-21T09:55:57.363 回答