set value from a custom dialog - android

i write a program that you can give location from user, then show him the location. for get location i use a custom dialog, but i don't know how use the location that user entered. for example i use a button for accept values but every time i use this method i get an error.
this is the source that i use a button and get error :
public class main extends MapActivity {
/** Called when the activity is first created. */
EditText lat;
EditText lang;
Integer latitude;
Integer longtitude;
Button b = (Button) findViewById(R.id.button1);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView view = (MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
final MapController control = view.getController();
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
latitude = Integer.valueOf(lat.getText().toString());
longtitude = Integer.valueOf(lang.getText().toString());
control.setCenter(new GeoPoint(latitude, longtitude));
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Dialog d = new Dialog(main.this);
d.setContentView(R.layout.choose);
d.setTitle("Location");
d.show();
lat = (EditText) findViewById(R.id.editText1);
lang = (EditText) findViewById(R.id.editText2);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
return super.onOptionsItemSelected(item);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
this is my permissions :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
so this is my problems :
why i get error and how i can fix that, i mean how i can use this button to close my custom dialog and set the location.
logcat :
05-23 13:15:10.953: W/KeyCharacterMap(13490): No keyboard for id 131074
05-23 13:15:10.953: W/KeyCharacterMap(13490): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-23 13:15:21.434: W/MapActivity(13490): Recycling dispatcher android_maps_conflict_avoidance.com.google.googlenav.datarequest.DataRequestDispatcher#40719228
05-23 13:15:21.444: V/MapActivity(13490): Recycling map object.
05-23 13:15:21.524: I/MapActivity(13490): Handling network change notification:CONNECTED
05-23 13:15:21.524: E/MapActivity(13490): Couldn't get connection factory client

Related

How to detect GPS being enabled

I'm making an app which uses GPS. The problem comes when you open the app without having GPS enabled, it does nothing (obviously) but if you enable GPS without exiting the app then the app still doing nothing. As I need to disable and enable GPS in my app I would need to know where the problem comes from, I'll leave you here some code:
public class EscuchaLocalizacion implements LocationListener {
private LocationManager gestor;
private TextView view1;
private TextView view2;
private boolean registrado;
private static final float minDistance = 10.0f;
private static final long minTime = 300;
public double _LONG;
public double _LAT;
EscuchaLocalizacion (Context c, TextView t1, TextView t2) {
Log.v("EscuchaLocalizacion", "Construyendo el objeto");
gestor = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
registrado = false;
view1 = t1;
view2 = t2;
Log.v("EscuchaLocalizacion", "He terminado de construir el objeto");
}
#Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
_LONG = arg0.getLongitude();
_LAT = arg0.getLatitude();
view1.setText(Double.toString(_LONG));
view2.setText(Double.toString(_LAT));
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
if(arg0.contentEquals(LocationManager.GPS_PROVIDER)){
Log.w("EscucharLocalizacion","Provider: "
+ LocationManager.GPS_PROVIDER + " was disabled");
}
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
if(!registrado && arg0.contentEquals(LocationManager.GPS_PROVIDER)){
registrar();
}
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
public void registrar() {
if (gestor.isProviderEnabled(LocationManager.GPS_PROVIDER)){
Log.i("EscucharLocalizacion", "Registrado a " + LocationManager.GPS_PROVIDER);
gestor.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
minDistance, this);
registrado = true;
}else {
Log.w("EscucharLocalizacion", "No se pudo registrar a " + LocationManager.GPS_PROVIDER);
registrado = false;
}
}
public void anularRegistro(){
gestor.removeUpdates(EscuchaLocalizacion.this);
registrado = false;
}
}
where do you initialize EscuchaLocalizacion?
You need to search location in onResume () and stop searching onPause location in your activity.
start locating:
escuchaLocalizacion.requestLocationUpdates(LocationManager.GPS_PROVIDER, 00, 0, gpsLocationListener);
stop locating:
escuchaLocalizacion.removeUpdates(gpsLocationListener);
If you have a checkbox to enable/disable GPS localisation, you can add a listener on the checkbox then put this functions there if you want to use GPS or not.
Also don't forget to add this in the manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

Updating Activity with Latitude/Longitude values returned from a Service

I'm trying to write a simple app that updates the MainActivity with the Lat/Lng values returned by the service. But it always returns the null value. I have added permissions and added TheService.java as service in AndroidManifest.xml file...Kindly tell me where I have gone wrong.
MainActivity.java
public class MainActivity extends Activity {
TextView tv1, tv2;
IntentFilter filter;
receive rec;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 =(TextView)findViewById(R.id.textView1);
tv2 = (TextView)findViewById(R.id.textView2);
filter = new IntentFilter("Updated");
rec = new receive();
Intent gps_int = new Intent(this,TheService.class);
startService(gps_int);
}
#Override
public void onPause()
{
super.onPause();
unregisterReceiver(rec);
}
#Override
public void onResume()
{
super.onResume();
rec = new receive();
registerReceiver(rec, filter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class receive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
tv1.setText(intent.getExtras().getString("lat"));
tv2.setText(intent.getExtras().getString("lon"));
Toast.makeText(getApplicationContext(), "Toast Executed", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "BR Latitude "+intent.getExtras().getString("lat"),Toast.LENGTH_SHORT).show();
}
}}
TheService.java
public class TheService extends Service implements LocationListener {
LocationManager lm;
Location loc;
double lat = 0;
double lon = 0;
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
lm=(LocationManager)getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
onLocationChanged(loc);
return START_STICKY;
}
#Override
public void onCreate()
{
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateui(location);
}
private void updateui(Location location) {
// TODO Auto-generated method stub
lat = location.getLatitude();
lon = location.getLongitude();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent gps_intent = new Intent("Updated");
gps_intent.putExtra("lat", lat);
gps_intent.putExtra("lon", lon);
sendBroadcast(gps_intent);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}}
You should use the Google Play Services for the location, that's easier to handle.
What for do you even need a Service? Having a location listener in your Activity is totally fine.
If you want to stay with the Service, than bind the Activity to it, instead of using a Broadcast.

Retrieve GPS location using android

I'm trying to get gps location of the current location using simple android program. I used onLocationChanged() method for this. But it returning the coordinates as 0.0. The users permission part is correct. This is the code of main one.
public class MainActivity extends Activity {
GPSTracker gps;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
final TextView tv = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
gps = new GPSTracker(MainActivity.this);
double lat = gps.latitude;
String mlat = String.valueOf(lat);
tv.setText(mlat);
}
});
}
}
this is the gps location class that im using...
public class GPSTracker extends Service implements LocationListener{
private final Context mContext;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
Location location;
double latitude;
double longtitude;
protected LocationManager locationManager;
public GPSTracker(Context context){
this.mContext = context;
getLocation();
}
public Location getLocation(){
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
return null;
}
protected void onStart() {
}
protected void onPause(){
locationManager.removeUpdates(this);
}
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
latitude = location.getLatitude();
longtitude = location.getLongitude();
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}

show Progress Dialog in OnLocationChanged

I know there are thousands of questions like this. But I can't find an explanation to mine.
I use the onLocationChanged method to update the user's location on a mapView. Everything's fine; I display a ProgressDialog in the onCreate method and dismiss it at the end of OnlocationChanged and it works.
The problem is when I create and show a Progress Dialog inside the onLocationChanged method. Somehow it doesn't work. I think it's because the method runs on a different thread.
But my question is, if the onLocationChanged method runs on a different thread, why does it let me to dismiss the dialog but not create a new one?
Here's part of my class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiendas);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
ProgressDialog dialog = ProgressDialog.show(StoresActivity.this, "",
"Getting your location", true, true);
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
/////if i show or initialize the dialog here, doesn't work!
updateMap(location) // this function calls asynctask
// for an HTTPrequest
dialog.dismiss();
}
}
That code works and dismisses the dialog correctly, but if I declare or show the dialog inside the onLocationChanged method, it never displays. Anybody?
Why does it dismiss it but can't show it?
Pass the Context of LocationListener class in ProgressDialog.
Check this code its working fine for me
public class LocationFinderActivity extends Activity implements LocationListener{
LocationManager locationManager = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_finder);
Log.i("inside","oncreate");
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_location_finder, menu);
return true;
}
#Override
public void onLocationChanged(Location location) {
Log.i("inside","onlocationchange");
ProgressDialog dialog = ProgressDialog.show(LocationFinderActivity.this, "",
"Getting your location", true, true);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

set Latitude and Longitude in map

i write a map viewer and i want to get Latitude and Longitude from user, then show him the location.
i use menu for this work. but i don't know how show him something to enter the values. this is my code.
public class main extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView view = (MapView) findViewById(R.id.themap);
view.setBuiltInZoomControls(true);
final MapController control = view.getController();
LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location location) {
control.setCenter(new GeoPoint((int)location.getLatitude(), (int)location.getLongitude()));
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
so i have 2 questions. first how can i create a menu for my locations. i must use context menu or something else.
second, if i get locations, i can enter them directly to my Latitude and Longitude?
in my code i use the default location of user.
Well if I am understanding you correctly you can do this 2 ways.
1) In your xml layout add an input box and button then on click set your location.
2) Override your onTouch and have and alert box pop up and ask for the long and lat. Then set it accordingly.
Both these will be setting your overLay: http://developer.android.com/resources/tutorials/views/hello-mapview.html
one of many overlay tutorials: http://eagle.phys.utk.edu/guidry/android/mapOverlayDemo.html
If u only want to show your current position yo can use MyLoicationOverlay class:
miPunto = new MyLocationOverlay(this, eMapa);
eMapa.getOverlays().add(miPunto);
miPunto.enableCompass();
miPunto.enableMyLocation();
If you wont to use this, yo must to get your location and then use an itemizedoverlay
This is my code, It works fine with me
Drawable marker=getResources().getDrawable(R.drawable.mapmarker);
marker.setBounds( (int) (-marker.getIntrinsicWidth()/2),
-marker.getIntrinsicHeight(),
(int) (marker.getIntrinsicWidth()/2),
0);
InterestingLocations funPlaces =
new InterestingLocations(marker);
Add your overlay Item in here.
mapView.getOverlays().add(funPlaces);
class InterestingLocations extends ItemizedOverlay {
private ArrayList<OverlayItem> locations =
new ArrayList<OverlayItem>();
private GeoPoint center = null;
public InterestingLocations(Drawable marker)
{
super(marker);
// Put your custom Lat, Long in here ,
GeoPoint disneySevenLagoon =
new GeoPoint((int)(28.410067*1000000),
(int)(-81.583699*1000000));
locations.add(new OverlayItem(disneySevenLagoon ,
"Seven Seas Lagoon", "Seven Seas Lagoon"));
populate();
}

Categories

Resources