0

我只是想做如果 gsm 位置更新,写入 cellid 数据库。为了这个愿望,我尝试实现服务类,如下所示。问题我应该怎么做才能自动检测到gsm位置并编写函数即C(),工作是将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);        
}
4

1 回答 1

1

您可以注册一个PhoneStateListener.LISTEN_CELL_LOCATION侦听器以获取 cellid 更新

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
      }
 }

}
于 2013-02-13T08:08:12.590 回答