【发布时间】:2012-11-18 01:02:16
【问题描述】:
你好 stackoverflow 的世界,
这是我在这里的第一篇文章。
我是 Android 世界的新手,所以如果我查看在线不同的教程,我在打开自己的地图时必须出现的启用 GPS 设置有问题。
我已经尝试了我在这里找到的以下代码:Android get location or prompt to enable location service if disabled
Button gpsButton = (Button)this.findViewById(R.id.buttonGPSLocation);
gpsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Start loction service
LocationManager locationManager = (LocationManager)[OUTERCLASS].this.getSystemService(Context.LOCATION_SERVICE);
Criteria locationCritera = new Criteria();
locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
locationCritera.setAltitudeRequired(false);
locationCritera.setBearingRequired(false);
locationCritera.setCostAllowed(true);
locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);
String providerName = locationManager.getBestProvider(locationCritera, true);
if (providerName != null && locationManager.isProviderEnabled(providerName)) {
// Provider is enabled
locationManager.requestLocationUpdates(providerName, 20000, 100, [OUTERCLASS].this.locationListener);
} else {
// Provider not enabled, prompt user to enable it
Toast.makeText([OUTERCLASS].this, R.string.please_turn_on_gps, Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
[OUTERCLASS].this.startActivity(myIntent);
}
}
});
但我收到了一些错误,可能是因为我也无法理解它!!!
这些是我的问题:
- 为什么需要声明一个按钮 (gpsButton) 以及我可以在哪里/如何创建它?
- 代码可以整体放在MapActivity类之后还是必须放在第一个onCreate之后?
- 什么是 OUTERCLASS?
- Manifest 上的部分权限(我已经拥有)我必须更改 Layout 上的某些内容?
所以,一般来说,有人可以更好地解释我如何使用此代码以及我需要做哪些修改。
如果您需要,我可以发送我的整个 Java 代码。
提前感谢您的光临。
关于,
克劳迪奥
【问题讨论】:
-
不鼓励在一个问题中提出多个问题
标签: android android-layout android-mapview android-maps