こんにちはスタックオーバーフロー:)
onStart(); 内にいくつかのコードを作成しました。ユーザーが GPS を有効にしていることを確認して、現在どの国にいるかを把握できるようにする方法。GPS が無効になっている場合は、アプリを使用する前に GPS を有効にするようユーザーに促すアラート ダイアログが表示されます。
何らかの理由で、コード全体が機能していないようです。GPS を無効にしましたが、何も起こりません。ダイアログも何もありません。なぜこうなった?
これが私のコードです:
@Override
protected void onStart() {
super.onStart();
LocationManager locationManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Geocoder code = new Geocoder(TipCalculatorActivity.this);
try {
Address adr = (Address) code.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
CountryName = adr.getCountryCode();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (!gpsEnabled) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires GPS connectivity to determine your current country, and deliver you accurate tip ratings. Do you wish to turn GPS on?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
TipCalculatorActivity.this.enableLocationSettings();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
TipCalculatorActivity.this.finish();
System.exit(1);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
private void enableLocationSettings() {
Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(settingsIntent);
}
助けてくれてありがとう:)