1)onCreate()
メソッドでLocationListener
、Activity に登録します。
// get location manager
LocationManager lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Button btnStart = (Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, YourActivity.this);
}
});
2)あなたの活動は以下を実装する必要がありますLocationListener
:
public class YourActivity extends Activity implements LocationListener {
3) のメソッドをオーバーライドLocationListener
:
@Override public void onLocationChanged(Location location) { // 位置が変更されたとき Log.d(TAG, "location changed.");
}
@Override
public void onProviderDisabled(String provider) {
// when the source (GSP or GSM network) are disabled
Log.d(TAG, "the source "+provider+" has been disabled");
}
@Override
public void onProviderEnabled(String provider) {
Log.i(TAG, "the source "+provider+" has been enabled");
//here you should test if the provider enabled is GPS , if yes , then launch your GPSActivity
if(provider.equals("gps") {
Intent gpslocation=new Intent(this,selection.class);
startActivity(gpslocation);
}
else {
//launch your alert dialog here
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i(TAG, "source status "+provider+" has been changed to : "+status);
}