2015-04-28 166 views
0

我試圖通過切換捕獲單元格ID。 我成功獲得了一個連接到我的手機的細胞ID。 但是,我無法完全解釋這一點,它似乎不同步。 我漫遊了幾次,但總是返回第一個單元格ID。GsmCellLocation返回相同的值

public class MainActivity extends Activity { 
private TextView textView; 
private GsmCellLocation gsmCellLocation; 
private TelephonyManager telephonyManager; 
private String cId = "", lac = ""; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textView = (TextView) findViewById(R.id.textView); 

    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
    gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation(); 
    telephonyManager.listen(listener, 
      PhoneStateListener.LISTEN_CELL_LOCATION); 

} 

private Handler handler = new Handler() { 

    @Override 
    public void handleMessage(Message msg) { 
     // TODO Auto-generated method stub 
     super.handleMessage(msg); 
     switch (msg.what) { 
     case 1: 
      updateText(); 
     } 
    } 

}; 


public String updateLocation() { 
    cId = Integer.toHexString(gsmCellLocation.getCid()); 
    lac = Integer.toString(gsmCellLocation.getLac()); 
    return "cId: " + cId + "\nLac: " + lac + "\n===============\n"; 
} 

public void updateText() { 
    Toast.makeText(getApplicationContext(), updateLocation(), 
      Toast.LENGTH_SHORT).show(); 
    textView.append(updateLocation()); 
} 

private PhoneStateListener listener = new PhoneStateListener() { 

    @Override 
    public void onCellLocationChanged(CellLocation location) { 
     // TODO Auto-generated method stub 
     super.onCellLocationChanged(location); 

     handler.sendEmptyMessage(1); 
    } 

}; 
} 

回答

1

你是不是應該加上:

MainActivity.this.gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation(); 

內onCellLocationChanged方法?

+0

謝謝!現在它的作品! – Bana