上篇布局搞完之后,现在要做的是第一个功能点:定位
骑行要知道天气,要知道天气就必须要定位,就要自动定位,所以要开启一个服务定位比较友好,所以:看代码
import android.Manifest;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;
/**
* @desc 定位服务
*
*/
public class LocationSvc extends Service implements LocationListener {
private static final String TAG = "LocationSvc";
private LocationManager locationManager;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
@Override
public void onStart(Intent intent, int startId) {
Log.e("onStart","intent === " + intent);
if (locationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} else if (locationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} else {
Toast.makeText(this, "无法定位", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean stopService(Intent name) {
return super.stopService(name);
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Get the current position \n" + location);
//通知Activity
Intent intent = new Intent();
intent.setAction(Common.LOCATION_ACTION);
intent.putExtra(Common.LOCATION, location);
sendBroadcast(intent);
// 如果只是需要定位一次,这里就移除监听,停掉服务。如果要进行实时定位,可以在退出应用或者其他时刻停掉定位服务。
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.removeUpdates(this);
stopSelf();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
在服务里面主要是实现了LocationListener 接口,获得Location之后通过intent发送给广播,注意这里要加几个权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.PERMISSION_GRANTED"/>
既然要发送给广播,所以我们要创建一个动态广播接受者,
class LocationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(Common.LOCATION_ACTION)) return;
Location locationInfo = (Location) intent.getParcelableExtra(Common.LOCATION);
Log.e("onReceive","locationInfo === " + locationInfo);
updateWithNewLocation(locationInfo);
dialog.dismiss();
HomeActivity.this.unregisterReceiver(this);// 不需要时注销
}
}
获取Location对象之后,就可以获取经纬度,在通过经纬度获取定位城市,
private void updateWithNewLocation(Location location) {
String coordinate;
String addressStr = "no address \n";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
coordinate = "Latitude:" + lat + "\nLongitude:" + lng;
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
Log.e("updateWithNewLocation","updateWithNewLocation ==222 " + geocoder);
try {
List<Address> addresses = geocoder.getFromLocation(latitude,
longitude, 1);
Log.e("updateWithNewLocation","updateWithNewLocation ==66 " + addresses.get(0).getLocality());
mCity.setText(addresses.get(0).getLocality());
} catch (IOException e) {
e.printStackTrace();
}
} else {
coordinate = "no coordinate!\n";
}
}
就是这样就可以获取城市信息,以及你周围的信息,List
addresses里面装载了很多数据,通过get方法获取国家,省份,城市,街道,周边信息。,在这里我只需要城市信息。