【发布时间】:2015-12-18 12:17:44
【问题描述】:
我是 Android 新手,我想在我的应用中查找我的位置,然后再查看我的位置我想查看我的 Gps 和 Internet 连接
ie:如果 Gps 未通过调用 Intent 启用,我们是呼叫设置,我们启用它同样,如果 Internet 移动数据未启用,我们如何通过 Intent 启用移动数据
例如如果我们想通过 Intent 打开 Gps 我们调用Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
lisewise 用于启用移动数据 我们称之为什么意图?
这是我的代码:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
checkGPSStatus();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public void onSearch(View view)
{
EditText location_tf = (EditText)findViewById(R.id.search);
String location = location_tf.getText().toString();
List<Address> addressList = null;
if(location != null || !location.equals(""))
{
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location , 1);
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude() , address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
// LatLng sydney = new LatLng(0, 0);
mMap.addMarker(new MarkerOptions().position(new LatLng(0,0)).title("Marker"));
}
private void checkGPSStatus() {
LocationManager locationManager = null;
boolean gps_enabled = false;
boolean network_enabled = false;
if ( locationManager == null) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
try {
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex){}
try {
network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex){}
if ( !gps_enabled && !network_enabled ){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("GPS not enabled");
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//this will navigate user to the device location settings screen
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
AlertDialog alert = dialog.create();
alert.show();
}
}
【问题讨论】:
-
如果 Gps 未启用通过调用 Intent 我们是呼叫设置并且我们启用它同样如果 Internet 移动数据未启用我们如何通过 Intent @2Dee 启用移动数据
标签: android gps android-mapview