<p>
1.默认gps定位</p><p>2.判断gps是否能定位,不能切换到network定位</p><p>3.监听gps定位,如果获取到gps信号,停止network定位</p>
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.citygis007.appdata.AppData;
import com.citygis007.gpsandjz.MainActivity;
import com.citygis007.gpsandjz.R;
import android.R.integer;
import android.R.string;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.NeighboringCellInfo;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.text.format.Time;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
public class WriteLog extends Service {
Timer timer=null;
boolean isRun=false;
private int seconds=30;
private boolean isGps=false;
private boolean isJizhan=false;
private String mfilename="";
private TelephonyManager mTelephonyManager;
private MyPhoneStateListener phoneStateListener;
private LocationManager locationManager;
private String provider;
private String TAG="WriteLog";
private Location location1;
private Location currentlocation=null;
String log1,log2,log3;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
// public class MyBinder extends Binder{
//
// public WriteLog getService(){
// return WriteLog.this;
// }
// }
//
// private MyBinder myBinder = new MyBinder();
@Override
public void onCreate() {
// TODO Auto-generated method stub
Log.i("service", "onCreate");
writeLog(AppData.fileTest, "onCreate\r\n");
super.onCreate();
SharedPreferences preferences=getApplicationContext().getSharedPreferences(AppData.stringValue, MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
mfilename=preferences.getString("filename", MainActivity.filename);
editor.putBoolean("isRun", true);
editor.commit();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//provider = locationManager.getProvider(LocationManager.GPS_PROVIDER).getName();
openGPS();
phoneStateListener=new MyPhoneStateListener();
}
@Override
public void onDestroy() {
Log.i("service", "onDestroy");
writeLog(AppData.fileTest, "onDestroy\r\n");
SharedPreferences preferences=getApplicationContext().getSharedPreferences(AppData.stringValue, MODE_PRIVATE);
SharedPreferences.Editor editor=preferences.edit();
editor.clear();
editor.commit();
isGps=false;
isJizhan=false;
if(timer!=null){
timer.cancel();
timer.purge();
}
stopGPS();
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
// if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null)
// {
// locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,this);
// }
// else if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null)
// {
// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,this);
// }
Log.i("service", "onStart");
writeLog(AppData.fileTest, "onStart\r\n");
if(intent==null){
Log.i("service", "intent null");
writeLog(AppData.fileTest, "intent null\r\n");
SharedPreferences