0

参考 LogCat 时出现错误。我已经存储了存储的simcard(1st)并与currentsimcard(2nd)进行比较,如果sim序列不同,logcat将打印出sim changed。但是当我运行它时,我的服务出现了问题。下面是我的代码

LogCat 显示

Tag SimSerial::            8944110065486249080  
Tag Current Sim Serial::   8944110065486249080  
Tag Sim Status             Sim no changed !!!  

Tag SimSerial::            8944110065486249080   
Tag Current Sim Serial::   8944110065486249080   
Tag Sim Status             Sim changed !!!

第一部分是正确的,但第二部分 Sim Status 也不应该是“Sim no changed”。有谁知道错误在哪里?

引导完成接收器

public class BootCompleteReceiver extends BroadcastReceiver {   

    Context context;
    SharedPreferences settings;
    public static final String PREFS_NAME = "MyPrefsFile";

     @Override  
    public void onReceive(Context context, Intent intent) {  

        Intent service = new Intent(context, MyService.class);  
        context.startService(service);   

    }  

}

我的服务

public class MyService extends Service {  

    String storedSimSerial;
    String currentSimSerial;

    @Override  
    public void onCreate() {  
        super.onCreate();    
    }  

    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  

        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();

        TelephonyManager telephoneMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        storedSimSerial = telephoneMgr.getSimSerialNumber();
        Log.e("SimSerial::",storedSimSerial);

        TelephonyManager tmMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        currentSimSerial = tmMgr.getSimSerialNumber();
        Log.e("Current Sim Serial::",currentSimSerial);

        if(currentSimSerial==storedSimSerial)
        {
            Log.e("Sim Status","Sim no changed !!!");
        }             
        else
            Log.e("Sim Status","Sim changed !!!");

        return Service.START_STICKY;  
    }  

    @Override
    public void onDestroy() {

        super.onDestroy();
        Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
    }

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

1 回答 1

1

像这样写你的if

    if(currentSimSerial.equals(storedSimSerial))
    {
        Log.e("Sim Status","Sim no changed !!!");
    }             
    else
        Log.e("Sim Status","Sim changed !!!");

当您使用时,==您比较对象引用,而不是很少适用于字符串的内容。

于 2013-01-24T00:22:32.047 回答