GPS based Alarm Application in Android
I want to create an application like:
- I am in city X
- I want to get notification(like ringing alarm or vibration etc) when I reach city Y.
I already have sqlite database containing information(latitude, longitude) about many cities.
I have searched Google and SO but didn't find proper solution.
So far my research says this:
What I have to do is to create a Service
or BroadcastReceiver
to get current location of the mobile phone using GPS.
Correct me if I am going wrong.
I want to get current position of mobile phone at an interval of 10mins. And based on that latitude longitude values I will compare that with the Scheduled city latitude longitude.
If match occurs then it should give notification to user.
Things that I have done:
I know how to get current location of user. (But not by creating service. Thats what I wanted to know! )
I am able to add tasks in sqlite database.
Please guide me how can I approach for this application.
Starting it when the user launches the application:
Intent i = new Intent(context, MyLocationApps.class);
context.startService(i);
public class MyLocationApps extends Service{
LocationManager locMan;
Location curLocation;
Boolean locationChanged;
LocationListener gpsListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Log.w("GPS", "Started");
if (curLocation == null) {
curLocation = location;
locationChanged = true;
}
if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude())
locationChanged = false;
else
locationChanged = true;
curLocation = location;
if (locationChanged)
locMan.removeUpdates(gpsListener);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
// Log.w("GPS", "Location changed", null);
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
if (status == 0)// UnAvailable
{
} else if (status == 1)// Trying to Connect
{
} else if (status == 2) {// Available
}
}
};
@Override
public void onCreate() {
Toast.makeText(getBaseContext(),"Inside onCreate of Service", Toast.LENGTH_LONG).show();
Log.e(TAG, "Inside onCreate of Service");
super.onCreate();
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, gpsListener);
/*if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 1, gpsListener);
} else {
this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
}
*/
if (curLocation != null) {
double lat = curLocation.getLatitude();
double lng = curLocation.getLongitude();
Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getBaseContext(),"Didn Get any location", Toast.LENGTH_LONG).show();
}
}
final String TAG="LocationService";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getBaseContext(),"Inside onStartCommand of Service", Toast.LENGTH_LONG).show();
Log.e(TAG, "Inside onStartCommand of Service");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onLowMemory() {
// TODO Auto-generated method stub
super.onLowMemory();
}
@Override
public void onStart(Intent i, int startId)
{
Toast.makeText(getBaseContext(),"Inside onStart of Service", Toast.LENGTH_LONG).show();
Log.e(TAG, "Inside onStart of Servic开发者_JAVA技巧e");
}
public IBinder onBind(Intent arg0) {
Log.e(TAG, "Inside onBind of Service");
return null;
}
}
Problem in code
When i run the application for the first time, it displays Toast Messages. But it never displays a Toast Message even i send location thru DDMS..
Where i am going wrong can you please tell me ?
Have a look at the code here : android-mycycle/MyLocationManager.java
I have implemented this for my application. I am trying to get a gps fix before I start receiving the location.
Finally i got it working : (Thanks to Ganapathy & Farhan )
public class MyLocationApps extends Service{
LocationManager locMan;
Location curLocation;
Boolean locationChanged;
Handler handler = new Handler();
LocationListener gpsListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Log.w("GPS", "Started");
if (curLocation == null) {
curLocation = location;
locationChanged = true;
}
if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude())
locationChanged = false;
else
locationChanged = true;
curLocation = location;
if (locationChanged)
locMan.removeUpdates(gpsListener);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
// Log.w("GPS", "Location changed", null);
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
if (status == 0)// UnAvailable
{
} else if (status == 1)// Trying to Connect
{
} else if (status == 2) {// Available
}
}
};
@Override
public void onCreate() {
Toast.makeText(getBaseContext(),"Inside onCreate of Service", Toast.LENGTH_LONG).show();
Log.e(TAG, "Inside onCreate of Service");
super.onCreate();
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, gpsListener);
/*if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,20000, 1, gpsListener);
} else {
this.startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
}
*/
if (curLocation != null) {
double lat = curLocation.getLatitude();
double lng = curLocation.getLongitude();
Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getBaseContext(),"Didn Get any location", Toast.LENGTH_LONG).show();
}
}
final String TAG="LocationService";
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getBaseContext(),"Inside onStartCommand of Service", Toast.LENGTH_LONG).show();
Log.e(TAG, "Inside onStartCommand of Service");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onLowMemory() {
// TODO Auto-generated method stub
super.onLowMemory();
}
@Override
public void onStart(Intent i, int startId)
{
Toast.makeText(getBaseContext(),"Inside onStart of Service", Toast.LENGTH_LONG).show();
Log.e(TAG, "Inside onStart of Service");
handler.postDelayed(GpsFinder,5000);// will start after 5 seconds
}
public IBinder onBind(Intent arg0) {
Log.e(TAG, "Inside onBind of Service");
return null;
}
public Runnable GpsFinder = new Runnable()
{
public void run()
{
// TODO Auto-generated method stub
if (locMan.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, gpsListener);
}
else
{
getApplicationContext().startActivity(new Intent("android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS"));
}
if (curLocation != null) {
double lat = curLocation.getLatitude();
double lng = curLocation.getLongitude();
Toast.makeText(getBaseContext(),"Lat : " + String.valueOf(lat) + "\n Long : "+ String.valueOf(lng), Toast.LENGTH_LONG).show();
}
handler.postDelayed(GpsFinder,5000);// register again to start after 5 seconds...
}
};
}
I think you should be using Geofence
along with intentservice
. Refer the official documentation for more information.
精彩评论