android network,gps定位切换

本文探讨了在Android平台上如何实现网络定位与GPS定位的切换,包括获取网络位置信息、启用和禁用GPS以及在应用中平滑过渡的策略。通过对LocationManager服务的使用,详细阐述了定位API的调用过程,同时关注了用户体验和电池效率。
摘要由CSDN通过智能技术生成

<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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值