packageacticitytest.exapmle.com.lbstest;importandroid.Manifest;importandroid.content.pm.PackageManager;importandroid.support.annotation.NonNull;importandroid.support.v4.app.ActivityCompat;importandroid.support.v4.content.ContextCompat;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.TextView;importandroid.widget.Toast;importcom.baidu.location.BDLocation;importcom.baidu.location.BDLocationListener;importcom.baidu.location.LocationClient;importcom.baidu.location.LocationClientOption;importjava.util.ArrayList;importjava.util.List;public class MainActivity extendsAppCompatActivity {publicLocationClient mLocationClient;privateTextView postionText;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationClient=newLocationClient(getApplicationContext());
mLocationClient.registerLocationListener(newMyLocationListener());
setContentView(R.layout.activity_main);
postionText=(TextView)findViewById(R.id.tv_positionTextView);
List permissionList=new ArrayList<>();if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_PHONE_STATE)!=PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}if(!permissionList.isEmpty()){
String [] permissions=permissionList.toArray(newString[permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
}else{
requestLocation();
}
}public voidrequestLocation(){
initLocation();
mLocationClient.start();//开始定位
}/**用于更新位置信息*/
private voidinitLocation(){
LocationClientOption option=newLocationClientOption();
option.setScanSpan(5000);//每五秒更新当前位置
mLocationClient.setLocOption(option);
}
@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {switch(requestCode){case 1:if(grantResults.length>0){for (intresult :grantResults){if (result!=PackageManager.PERMISSION_GRANTED){
Toast.makeText(this,"必须同意所有权限才能使用本程序",Toast.LENGTH_SHORT).show();
finish();return;
}
}
requestLocation();
}else{
Toast.makeText(this,"未知错误",Toast.LENGTH_SHORT).show();
finish();
}break;default:break;
}
}public class MyLocationListener implementsBDLocationListener{
@Overridepublic voidonReceiveLocation(BDLocation bdLocation) {
StringBuilder currentPostion=newStringBuilder();
currentPostion.append("纬度:").append(bdLocation.getLatitude()).append("\n");
currentPostion.append("经度:").append(bdLocation.getLongitude()).append("\n");
currentPostion.append("定位方式:");if (bdLocation.getLocType()==BDLocation.TypeGpsLocation){
currentPostion.append("GPS");
}else if(bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){
currentPostion.append("网络");
}else{
currentPostion.append("未识别");
}
postionText.setText(currentPostion);
}
}
@Overrideprotected voidonDestroy() {super.onDestroy();
mLocationClient.stop();//不断更新时一定要注意,在活动销毁时关闭,否则会一直耗电
}
}