I've currently finished learning the basics of android development and am trying to learn to make an android gps app in smartphones for my traffic system project. Not so very good yet in doing complicated codes. Please advise some resources or tutorials which will greatly help me to make this app. - the app will send gps locations( lat & longitude 开发者_开发问答and time ) via sms every 10 seconds once it approach a specific road section ( can be like 0.5 km length of road) - if the phone passed that specific road section it will stop sending its locations
Why are you using the sms service to send the location? try using the webservice.
To implement this app you has to use the LocationManager and LocationListener libraries. You can start by creating a gps listening class implementing the LocationListener, like the one shown below
public class CTLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
Log.w("LOCATION CHANGED", ""+location);
if(location != null) {
Constants.LATTITUDE = location.getLatitude();
Constants.LONGITUDE = location.getLongitude();
Constants.kAccuracy = location.getAccuracy();
Constants.ALTITUDE_VALUE = location.getAltitude();
}
}
@Override
public void onProviderDisabled(String provider) {
Log.i("PROVIDER", "DISABLED:"+provider);
}
@Override
public void onProviderEnabled(String provider) {
Log.i("PROVIDER", "ENABLED:"+provider);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("EXTRAS", ""+extras);
Log.i("Provider status", ""+status);
}
}
Here I'm storing the updated locations on the Constants file. Start a sheduler that checks the locations periodically and start sending messages if they match your required locations.
To trigger the gps you can use
LocationListener locationListener = new CTLocationListener();
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);
The scheduler you need is a timertask. This can be implemented as
public class showAccuracy extends TimerTask {
@Override
public void run() {
ghandler.post(new Runnable() {
@Override
public void run() {
if((Constants.Latitude == yourlatitude) && .....) {
} } }); }
}
public class HomeActivity extends Activity implements LocationListener{
public static Context mContext;
private double latitude, longitude;
public LocationManager mLocManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
mContext=this;
super.onCreate(savedInstanceState);
setContentView(R.layout.homelayout);
mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
this);
mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
0, this);
locationUpdate();
((Button) this.findViewById(R.id.ButtonHome))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
startActivity(new Intent(HomeActivity.this,
DefaultDisplay.class));
}
});
((Button) this.findViewById(R.id.ButtonProfile))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (GUIStatics.boolLoginStatus) {
startActivity(new Intent(HomeActivity.this,
MyProfile.class));
} else {
Intent intent=new Intent(HomeActivity.this,
Login.class);
intent.putExtra("moveTo","MyProfile");
startActivity(intent);
}
}
});
((Button) this.findViewById(R.id.ButtonNotifications))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (GUIStatics.boolLoginStatus) {
startActivity(new Intent(HomeActivity.this,
ShowAllNotificationActiviry.class));
} else {
Intent intent=new Intent(HomeActivity.this,
Login.class);
intent.putExtra("moveTo","ShowAllNotificationActiviry");
startActivity(intent);
}
}
});
((Button) this.findViewById(R.id.ButtonFavorites))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (GUIStatics.boolLoginStatus) {
startActivity(new Intent(HomeActivity.this,
FavoritesActivity.class));
} else {
Intent intent=new Intent(HomeActivity.this,
Login.class);
intent.putExtra("moveTo","FavoritesActivity");
startActivity(intent);
}
}
});
((Button) this.findViewById(R.id.ButtonMore))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
startActivity(new Intent(HomeActivity.this,
MoreListActivity.class));
}
});
}
public void locationUpdate()
{
CellLocation.requestLocationUpdate();
}
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
GUIStatics.currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
GUIStatics.latitude = obj.getLatitude();
GUIStatics.longitude = obj.getLongitude();
GUIStatics.currentCity= obj.getSubAdminArea();
GUIStatics.currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
GUIStatics.latitude=location.getLatitude();
GUIStatics.longitude= location.getLongitude();
Log.v("Test", "IGA" + "Lat" + latitude + " Lng" + longitude);
//mLocManager.r
getAddress(latitude, longitude);
if(location!=null)
{
mLocManager.removeUpdates(this);
}
// Toast.makeText(this, "Lat" + latitude + " Lng" + longitude,
// Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Toast.makeText(HomeActivity.this, "Gps Disabled", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
if(arg1 ==
LocationProvider.TEMPORARILY_UNAVAILABLE) {
Toast.makeText(HomeActivity.this,
"LocationProvider.TEMPORARILY_UNAVAILABLE",
Toast.LENGTH_SHORT).show();
}
else if(arg1== LocationProvider.OUT_OF_SERVICE) {
Toast.makeText(HomeActivity.this,
"LocationProvider.OUT_OF_SERVICE", Toast.LENGTH_SHORT).show();
}
}
}
This is the code use for getting latitude and longitude or the device and also got the all information about the lat long using get address function .
here some other URL is very use full to you. http://developer.android.com/reference/android/location/LocationManager.html http://code.google.com/p/open-gpstracker/ http://androidcommunity.com/forums/f4/android-gps-support-and-info-55/ http://www.devx.com/wireless/Article/43005 http://android-er.blogspot.com/2011/02/get-locationlatitude-and-longitude-from.html
I hope this is very useful to you.
精彩评论