2012-05-21 53 views
0

服務類比較SIM卡的IMSI在開機

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不同,那麼應用程序會在啓動後將用戶帶到另一個活動中......我的編碼有什麼問題?

回答

0

你沒有比較IMSI,你是比較IMEI(這是手機的ID,它不會改變)。

爲了得到IMSI你需要:

IMSI = m_telephonyManager.getSubscriberId(); 
0

是,getSubscriberId()是正確的方法,並順便說一下,當您收到boot_completed播出後,「IMSI」通常是不可用的。