Android LocationManager GPS Reading Never Changes While Screen is Off - android

I am trying to log a GPS location while the screen is off.
Here is my manifest file (note the WAKE_LOCK and GPS permissions as well as the LocationService definition):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.joshuajwitter.backgroundgpstest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BackgroundGPSTestActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".service.LocationService" >
<intent-filter>
<action android:name="com.joshuajwitter.backgroundgpstest.service.LocationService" />
</intent-filter>
</service>
</application>
</manifest>
Here is my background service:
package com.joshuajwitter.backgroundgpstest.service;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
public class LocationService extends Service {
private static final String TAG = "LocationService";
// the location manager and listener
LocationManager m_locationManager;
GPSLocationListener m_gpsLocationListener;
// the dreaded wakelock
WakeLock m_wakeLock;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onStart(Intent intent, int startId) {
Log.d(TAG, "Starting the LocationService");
// acquire the wakelock
Log.d(TAG, "Acquiring the wake lock");
m_wakeLock.acquire();
}
#Override
public void onCreate() {
// get the wakelock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
m_wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"String Wake Lock");
// instantiate the gps listener
m_gpsLocationListener = new GPSLocationListener();
// get the location manager
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// request location updates every 5 seconds
m_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 0, m_gpsLocationListener);
}
#Override
public void onDestroy() {
Log.d(TAG, "Stopping the LocationService");
// if the wakelock is held
if (m_wakeLock.isHeld()) {
// release it
Log.d(TAG, "Releasing the wake lock");
m_wakeLock.release();
}
// remove the listener
m_locationManager.removeUpdates(m_gpsLocationListener);
}
private class GPSLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Got a new location frome the LocationManager [" + location.getLatitude()
+ ", " + location.getLongitude() + "]");
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
..and here is the activity that starts and stops the service:
package com.joshuajwitter.backgroundgpstest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.joshuajwitter.backgroundgpstest.service.LocationService;
public class BackgroundGPSTestActivity extends Activity {
private boolean m_serviceRunning = false;
private Button m_mainButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get the main button
m_mainButton = ((Button) findViewById(R.id.mainButton));
// set the main button listener
m_mainButton
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// if the service is not running
if (!m_serviceRunning) {
// start the service
startService();
}
else {
// start the service
stopService();
}
}
});
}
private void startService() {
m_serviceRunning = true;
startService(new Intent(this, LocationService.class));
m_mainButton.setText("Stop Background GPS Service");
}
private void stopService() {
m_serviceRunning = false;
stopService(new Intent(this, LocationService.class));
m_mainButton.setText("Start Background GPS Service");
}
}
When I run the service and keep the screen on, I get the correct GPS latitude and longitude for my position. When I turn the screen off, however, I get the same repeating output:
01-31 15:48:58.467: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:48:59.467: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:49:00.467: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:49:01.467: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:49:02.477: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:49:03.477: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
01-31 15:49:04.467: D/LocationService(11752): Got a new location frome the LocationManager [-36.879621, -7.734375]
When I turn the screen back on, I get correct GPS values once more. Here's the kicker: Google Maps Navigation runs on my phone with the screen off. If I run it at the same time as my test program, the same behavior occurs... the Navigation app gets notified of the correct coordinates while my test app repeats the same lat/long over and over.
This is on an Android phone running 2.3.3 Gingerbread. Thanks for any input!

Related

Get Location From Offline Application

I'd like to get Geo location by using GPS provider. So my code will be like this
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
LocationManager lm;
TextView textLatitude, textLongitude;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLatitude = (TextView)findViewById(R.id.textLatitude);
textLongitude = (TextView)findViewById(R.id.textLongitude);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
/* Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);*/
}
public void onResume() {
super.onResume();
setup();
}
public void onStart() {
super.onStart();
boolean gpsEnabled, networkEnabled;
gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.d("LocationService","GPS Status: "+gpsEnabled);
if(!gpsEnabled) {
networkEnabled =
lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!networkEnabled) {
Intent intent =
new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
}
}
public void onStop() {
super.onStop();
lm.removeUpdates(listener);
}
public void setup() {
lm.removeUpdates(listener);
String latitude = "Unknown";
String longitude = "Unknown";
Location gpsLocation = requestUpdatesFromProvider(
LocationManager.GPS_PROVIDER, "GPS not supported");
if(gpsLocation != null) {
Log.d("Location Service","GET FROM GPS");
latitude = String.format("%.7f", gpsLocation.getLatitude());
longitude = String.format("%.7f", gpsLocation.getLongitude());
}
textLatitude.setText(latitude);
textLongitude.setText(longitude);
}
public Location requestUpdatesFromProvider(final String provider
, String error) {
Location location = null;
if (lm.isProviderEnabled(provider)) {
lm.requestLocationUpdates(provider, 0, 0, listener);
Log.d("LocationService","Requesting...");
location = lm.getLastKnownLocation(provider);
//Log.d("LocationSerivce", "Geo :"+location.getLatitude());
} else {
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
}
return location;
}
public LocationListener listener = new LocationListener() {
public void onLocationChanged(Location location) {
textLatitude.setText(String.format("%.7f", location.getLatitude()));
textLongitude.setText(String.format("%.7f",location.getLongitude()));
}
public void onProviderDisabled(String provider) { }
public void onProviderEnabled(String provider) { }
public void onStatusChanged(String provider
, int status, Bundle extras) { }
};
}
The result is ... IT DOESN'T WORK. What have I done wrong?
I've also added permission to manifest file already.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name
="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.locationservice.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I search a lot from Internet and I got that it'll work if I combine with Network provider and I have to use internet to get my location. This is not what I want.
I want to get Location from GPS only because I'll use for offline application.
Could you give me any great suggestion. I'll thanks a lot

My android location service killed when app switched to background

Please apologize me if my question is repeated or simple. Struggling in this issue for a long time.
I need to track user's location even when my app is switched to background. On surfing I found that the location processor code can be written as a service, so that the service will not be killed and we can get the user's location (in both status - app in foreground and when app runs in background).
When my app is in foreground, I was able to track user's location continuously. However, when I switched the app to background, I feel my location service dies and I was not able to track user's location. Please find my code below:
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testmylocation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService"/>
</application>
</manifest>
MainActivity.java:
package com.example.testmylocation;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.Window;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
startService(new Intent(this, MyService.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyService.java:
package com.example.testmylocation;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
public class MyService extends Service
{
private static final String TAG = "TestMyLocation";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 0;
private static final float LOCATION_DISTANCE = 0;
long itsBatchId = 0;
private class LocationListener implements android.location.LocationListener
{
Location mLastLocation;
public LocationListener(String provider)
{
mLastLocation = new Location(provider);
}
#Override
public void onLocationChanged(Location location)
{
Thread aThread = new Thread(new Runnable() {
#Override
public void run() {
sendLocationValues(location);
}
});
aThread.start();
}
#Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled: " + provider);
}
#Override
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled: " + provider);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged: " + provider);
}
}
private void sendLocationValues(Location theLocation)
{
//A web service will be called and the user's current location will be stored in server
}
LocationListener[] mLocationListeners = new LocationListener[]
{
new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER)
};
#Override
public IBinder onBind(Intent arg0)
{
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
#Override
public void onCreate()
{
initializeLocationManager();
itsLocationHandler.sendEmptyMessage(1);
}
private Handler itsLocationHandler = new Handler()
{
#Override
public void handleMessage(Message theMessage)
{
if(theMessage.what == 1)
{
try
{
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, mLocationListeners[1]);
}
catch (java.lang.SecurityException ex)
{
Log.i(TAG, "fail to request location update, ignore", ex);
}
catch (IllegalArgumentException ex)
{
Log.d(TAG, "network provider does not exist, " + ex.getMessage());
}
}
}
};
#Override
public void onDestroy()
{
super.onDestroy();
}
private void initializeLocationManager()
{
if (mLocationManager == null)
{
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
}
}
}
Can anyone please correct what I am doing wrong.
Thank you.
Since Android 4.0, the OS has gotten much more agressive about killing off unnecessary processes. If you need to track the user's location even when your app is in the background, then you need to declare your service as a foreground service. This raises the priority of your service so that Android is unlikely to kill it unless it really needs the resources. See Running a service in the foreground for details about how to do this.
location processor code can be written as a service, so that the service will not be killed
There is nothing can guarntee that the service will never be killed, however you can increase the likelihood that your service will continue running by obtaining WakeLock and start it as Foreground service
I would recommend you check few open source projects such as Open GPS Tracker and Traceper

A basic Android Service that uses GPS is causing Google Nexus One to reboot

I have created a very basic program to track GPS coordinates. The program has a button that turns a service on and off. The service simple installs a LocationListener. On each onLocationChanged the location coordinates are dumped on sd card.
The application works fine. But when I push the sleep button, the application logs data for sometime and then causes Google Nexus One to reboot.
Below I am posting the MINIMUM code that is required to reproduce this bug. I have striped extra code to avoid confusion.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phonetracker"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".PhoneTrackerActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyGPSService" android:process=":my_gps_service" />
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/on_button_click"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/on_button_click"
android:onClick="onButtonClick" />
</LinearLayout>
values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PhoneTrackerActivity!</string>
<string name="app_name">PhoneTracker</string>
<string name="on_button_click">Start Service</string>
</resources>
PhoneTrackerActivity.java
package com.phonetracker;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class PhoneTrackerActivity extends Activity
{
private static final String TAG = "MyGPSServiceDemoActivity";
private Intent intent = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.on_button_click);
if (isMyServiceRunning())
{
button.setText("Stop Service");
}
else
{
button.setText("Start Service");
}
if (intent == null)
intent = new Intent(this, MyGPSService.class);
}
public void onButtonClick(View view)
{
Log.e(TAG, "onButtonClick");
Button button = (Button)view;
if (isMyServiceRunning())
{
stopService(intent);
button.setText("Start Service");
}
else
{
startService(intent);
button.setText("Stop Service");
}
}
private boolean isMyServiceRunning()
{
Log.e(TAG, "isMyServiceRunning");
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
{
if ("com.gpsservicedemo.MyGPSService".equals(service.service.getClassName()))
{
return true;
}
}
return false;
}
}
MyGPSService.java
package com.phonetracker;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.IBinder;
import android.util.Log;
public class MyGPSService extends Service
{
private static final String TAG = "Hx2MyGPSService";
private LocationManager locationManager = null;
private GPSLocationListener gpsLocationListener = null;
#Override
public IBinder onBind(Intent arg0)
{
Log.e(TAG, "onBind");
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
#Override
public void onCreate()
{
Log.e(TAG, "onCreate");
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
gpsLocationListener = new GPSLocationListener();
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);
}
}
#Override
public void onDestroy()
{
Log.e(TAG, "onDestroy");
super.onDestroy();
if (locationManager != null)
{
try
{
locationManager.removeUpdates(gpsLocationListener);
}
catch (Exception ex)
{
Log.e(TAG, "fail to remove location listners, ignore", ex);
}
}
gpsLocationListener.closeOperations();
}
}
GPSLocationListener.java
package com.phonetracker;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
public class GPSLocationListener implements LocationListener
{
private static final String TAG = "Hx2GPSLocationListener";
public GPSLocationListener ()
{
Log.e(TAG, "GPSLocationListener");
}
#Override
public void onLocationChanged(Location location)
{
Log.e(TAG, "onLocationChanged");
}
#Override
public void onProviderDisabled(String provider)
{
Log.e(TAG, "onProviderDisabled");
}
#Override
public void onProviderEnabled(String provider)
{
Log.e(TAG, "onProviderEnabled");
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.e(TAG, "onStatusChanged");
}
public void closeOperations()
{
Log.e(TAG, "closeOperations");
}
}
Please see if I am missing out something.

gps monitoring service in android

I'm trying to create a program for android that constantly (every minute) gets the gps location and then sends it to my server at home (so that i always know where my phone is).
I created a gui with a start-button which starts the service:
start.setOnClickListener(new View.OnClickListener() {
synchronized public void onClick(View v) {
startService(new Intent(GpsTest2.this, GTService.class));
}
});
Then my service is declared like this:
public class GTService extends Service implements LocationListener {
}
This GTService has a method for retrieving the data:
public void onLocationChanged(Location location) {
}
#Override
public void onCreate() {
super.onCreate();
LocationManager locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval * 1000, minDist, this);
}
In AndroidManifest.xml I have:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<service android:name=".GTService">
</service>
This doesn't seem to work: no data is logged.
What am I doing wrong here?
Your code seems to be working fine except this,
You are using LocationManager.NETWORK_PROVIDER
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, interval * 1000, minDist, this);
Where as it should be LocationManager.GPS_PROVIDER
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2 * 1000, 10, locationListener);
Hope this will surely do for you. Thanks.
The location from Network provider is not accurate. PLease use GPS instead. If you really want to user Network only then I would recomond to set minDistance param as 0
The following should work fine,
Manifest :
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.smartconcept.locationmonitor.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".GTService" />
</application>
GTService :
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;
public class GTService extends Service implements LocationListener {
LocationManager locMgr;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate(){
super.onCreate();
locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1 * 1000, 0, this);
}
#Override
public void onDestroy(){
super.onDestroy();
locMgr.removeUpdates(this);
}
#Override
public void onLocationChanged(Location loc) {
Toast.makeText(getBaseContext(), String.valueOf(loc.getLatitude()) + "\n" + String.valueOf(loc.getLongitude()), Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
MainActivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart = (Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
startService(new Intent(MainActivity.this,GTService.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Hope this helps.

Auto Sends GPS Coordinates via SMS

I made an Application that shows my GPS coordinates (Langitude and Latitude) number, which designed with out a map. But I would like to send the lat and long to mobile number/phone number Automatically (no need a send button) every time I open this Application. Anyone can help me..
Gps.java
package Sample.gps.send;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class Gps extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
#Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My location is: " +
"Latitude = " + loc.getLatitude() +
"Longitude = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"GPS Disabled",
Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"GPS Enabled",
Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
}
Here is for the AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Sample.gps.send"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon"
android:label="#string/app_name">
<activity android:name=".UseGps"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
</manifest>
Here MyLocationListener is an inner class. Can anyone tell how to retrieve the coordinates whenever onLocationChanged method is called? The location is displayed using a Toast. Is it possible to get the coordinates by calling any method from onCreate()?
See, http://mobiforge.com/developing/story/sms-messaging-android. That tutorial will show you how to send an SMS, using that you should be able to figure out what you need to do.
Please use the code tags when posting code samples :-)

Categories

Resources