我应该怎么做才能自动检测到gsm位置并调用写入函数?

时间:2013-02-13 08:01:27

标签: android

如果更新了gsm位置,我只想这样做,将cellid写入数据库。为此,我尝试实现服务类,如下所示。我应该怎样做才能使gsm位置被自动检测并写入函数即C(),job是将cellid写入数据库?

我的开始工作;

public class GpsCell extends Service{
TelephonyManager tel;

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

    tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    GsmCellLocation location = (GsmCellLocation)telephonyManager.getCellLocation();

}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
    return super.onStartCommand(intent, flags, startId);        
}

1 个答案:

答案 0 :(得分:1)

您可以注册PhoneStateListener.LISTEN_CELL_LOCATION侦听器以获取小工具更新

public class GpsCellLogger extends Service{
  TelephonyManager tel;

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

    tel = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

    tel.listen(myListener , PhoneStateListener.LISTEN_CELL_LOCATION);

    GsmCellLocation location = (GsmCellLocation)telephonyManager.getCellLocation();


  }
  PhoneStateListener myListener = new PhoneStateListener() {
      public void onCellLocationChanged(CellLocation p_CellLocation)
      {
        //write to database
      }
 }

}