引用LogCat时出错。我存储了存储卡(1st)并与currentsimcard(2nd)进行比较,如果sim串口不同,logcat将打印出sim更改。但是当我运行它时,我的服务有问题。以下是我的代码
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状态应该没有" Sim没有改变"同样。 有谁知道错误在哪里?
BootCompleteReceiver
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);
}
}
为MyService
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;
}
}
答案 0 :(得分:1)
像这样写if
:
if(currentSimSerial.equals(storedSimSerial))
{
Log.e("Sim Status","Sim no changed !!!");
}
else
Log.e("Sim Status","Sim changed !!!");
当你使用==
时,你比较对象引用,而不是很少用于字符串的内容。