30 | * 关于高德定位SDK的使用可以参考高德开放平台官网:https://lbs.amap.com/api/android-location-sdk/locationsummary/ 31 | *
32 | *33 | * 示例中主要展示通过自己实现ForcegroundService的方式实现. 34 | * 高德定位SDK从3.8.0开始已经开支支持直接将APSService变成前台service, 35 | * 对应的接口名称分别是: 36 | * 开启后台定位:{@link AMapLocationClient#enableBackgroundLocation(int, Notification)} 37 | * 关闭后台定位能力:{@link AMapLocationClient#disableBackgroundLocation(boolean)} 38 | * 在代码示例里已经有实现了,这个功能与使用自己实现ForcegroundService的方式使用其中一种就可以了,感兴趣的可以试试这个功能。 39 | *
40 | * @author hongming.wang 41 | */ 42 | public class MainActivity extends CheckPermissionsActivity 43 | implements 44 | OnCheckedChangeListener, 45 | OnClickListener { 46 | private RadioGroup rgLocationMode; 47 | private EditText etInterval; 48 | private EditText etHttpTimeout; 49 | private CheckBox cbOnceLocation; 50 | private CheckBox cbAddress; 51 | private CheckBox cbGpsFirst; 52 | private CheckBox cbCacheAble; 53 | private CheckBox cbOnceLastest; 54 | private CheckBox cbSensorAble; 55 | private TextView tvResult; 56 | private Button btLocation; 57 | 58 | private AMapLocationClient locationClient = null; 59 | private AMapLocationClientOption locationOption = null; 60 | 61 | Intent serviceIntent = null; 62 | @Override 63 | protected void onCreate(Bundle savedInstanceState) { 64 | super.onCreate(savedInstanceState); 65 | setContentView(R.layout.activity_main); 66 | setTitle(R.string.title_location); 67 | 68 | /** 69 | * 如果使用{@link AMapLocationClient#enableBackgroundLocation(int, Notification)}的方式实现 70 | * 可以不用这个service 71 | */ 72 | serviceIntent = new Intent(); 73 | serviceIntent.setClass(this, LocationForcegroundService.class); 74 | 75 | initView(); 76 | 77 | //初始化定位 78 | initLocation(); 79 | } 80 | 81 | //初始化控件 82 | private void initView(){ 83 | rgLocationMode = (RadioGroup) findViewById(R.id.rg_locationMode); 84 | 85 | etInterval = (EditText) findViewById(R.id.et_interval); 86 | etHttpTimeout = (EditText) findViewById(R.id.et_httpTimeout); 87 | 88 | cbOnceLocation = (CheckBox)findViewById(R.id.cb_onceLocation); 89 | cbGpsFirst = (CheckBox) findViewById(R.id.cb_gpsFirst); 90 | cbAddress = (CheckBox) findViewById(R.id.cb_needAddress); 91 | cbCacheAble = (CheckBox) findViewById(R.id.cb_cacheAble); 92 | cbOnceLastest = (CheckBox) findViewById(R.id.cb_onceLastest); 93 | cbSensorAble = (CheckBox)findViewById(R.id.cb_sensorAble); 94 | 95 | tvResult = (TextView) findViewById(R.id.tv_result); 96 | btLocation = (Button) findViewById(R.id.bt_location); 97 | 98 | rgLocationMode.setOnCheckedChangeListener(this); 99 | btLocation.setOnClickListener(this); 100 | } 101 | 102 | @Override 103 | protected void onPause() { 104 | super.onPause(); 105 | //如果已经开始定位了,显示通知栏 106 | if(isSartLocation) { 107 | //如果使用{@link AMapLocationClient#enableBackgroundLocation(int, Notification)},这段代码可以不要 108 | if (null != serviceIntent) { 109 | startService(serviceIntent); 110 | } 111 | 112 | //开启APSService后台定位能力 113 | // if(null != locationClient) { 114 | // locationClient.enableBackgroundLocation(Utils.NOTIFY_ID, Utils.buildNotification(getApplicationContext())); 115 | // } 116 | } 117 | } 118 | 119 | @Override 120 | protected void onResume() { 121 | super.onResume(); 122 | /** 123 | * 如果要一直显示可以不执行 124 | * 如果使用{@link AMapLocationClient#disableBackgroundLocation(boolean)} ,这段代码可以不要 125 | */ 126 | if(null != serviceIntent){ 127 | stopService(serviceIntent); 128 | } 129 | 130 | //关闭APSService后台定位能力 131 | // if(null != locationClient) { 132 | // locationClient.disableBackgroundLocation(true); 133 | // } 134 | } 135 | 136 | 137 | @Override 138 | protected void onDestroy() { 139 | super.onDestroy(); 140 | destroyLocation(); 141 | 142 | //如果要一直显示可以不执行 143 | if(null != serviceIntent){ 144 | stopService(serviceIntent); 145 | } 146 | 147 | } 148 | 149 | @Override 150 | public void onCheckedChanged(RadioGroup group, int checkedId) { 151 | if (null == locationOption) { 152 | locationOption = new AMapLocationClientOption(); 153 | } 154 | switch (checkedId) { 155 | case R.id.rb_batterySaving : 156 | locationOption.setLocationMode(AMapLocationMode.Battery_Saving); 157 | break; 158 | case R.id.rb_deviceSensors : 159 | locationOption.setLocationMode(AMapLocationMode.Device_Sensors); 160 | break; 161 | case R.id.rb_hightAccuracy : 162 | locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy); 163 | break; 164 | default : 165 | break; 166 | } 167 | 168 | } 169 | 170 | /** 171 | * 设置控件的可用状态 172 | */ 173 | private void setViewEnable(boolean isEnable) { 174 | for(int i=0; i