package
com.example.ex_0428;
import
android.app.AlertDialog.Builder;
import
android.content.Context;
import
android.content.DialogInterface;
import
android.content.DialogInterface.OnClickListener;
import
android.location.Location;
import
android.location.LocationListener;
import
android.location.LocationManager;
import
android.os.Bundle;
import
android.support.v7.app.ActionBarActivity;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
Ex1LocActivity
extends
ActionBarActivity {
TextView ex1_txt;
double
latitude, longitude;
LocationManager l_manager;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex1_loc);
ex1_txt = (TextView)findViewById(R.id.ex1_txt);
checkGPS();
}
private
void
checkGPS()
{
Builder a_dialog =
new
Builder(Ex1LocActivity.
this
);
a_dialog.setTitle(
"Location"
);
a_dialog.setMessage(
"use GPS??\n\n outside? \"YES\" \n inside? \"NO\" "
);
a_dialog.setPositiveButton(
"Yes"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
init(
true
);
}
});
a_dialog.setNegativeButton(
"NO"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
init(
false
);
}
});
a_dialog.setCancelable(
false
);
a_dialog.show();
}
private
void
init(
boolean
isGPS) {
String provider =
null
;
l_manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if
(isGPS) {
provider = LocationManager.GPS_PROVIDER;
}
else
{
provider = LocationManager.NETWORK_PROVIDER;
}
Toast.makeText(getApplicationContext(), provider,
1000
).show();
l_manager.requestLocationUpdates(provider,
1000
,
1
, l_listener);
}
LocationListener l_listener =
new
LocationListener() {
@Override
public
void
onStatusChanged(String provider,
int
status, Bundle extras) {
}
@Override
public
void
onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(),
"find provider"
,
1000
).show();
}
@Override
public
void
onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(),
"no found provider"
,
1000
).show();
}
@Override
public
void
onLocationChanged(Location location) {
String str =
null
;
Toast.makeText(getApplicationContext(),
"onLocationChanged() called"
,
1000
).show();
latitude = location.getLatitude();
longitude = location.getLongitude();
if
(location !=
null
)
{
str =
"Latitude : "
+ latitude +
"\nLongitude : "
+ longitude;
}
else
{
Toast.makeText(getApplicationContext(),
"onLocationChanged() : location is null"
,
1000
).show();
}
ex1_txt.setText(str);
}
};
protected
void
onDestroy() {
super
.onDestroy();
if
(l_manager !=
null
)
l_manager.removeUpdates(l_listener);
};
}