I'm making this simple app where when a button is placed users gps location should be displayed and it works fine with emulator but when i try that in a real phone both longitude and latitude are zero.
this is my code:
Main Activiy:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView longS = (TextView) findViewById(R.id.longi);
final TextView latiS = (TextView) findViewById(R.id.lati);
Button btn_show_location = (Button) findViewById(R.id.button1);
btn_show_location.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
LocationManager mlocManager=null;
LocationListener mlocListener;
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
if(MyLocationListener.latitude>0)
{
longS.setText("Latitude:- " + MyLocationListener.latitude);
latiS.setText("Longitude:- " + MyLocationListener.longitude + '\n');
}
else
{
longS.setText("Please wait");
}
} else {
Intent gpsOptionsIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(gpsOptionsIntent);
}
}
});
}
MyLocationListener:
public class MyLocationListener implements LocationListener {
public static double latitude;
public static double longitude;
#Override
public void onLocationChanged(Location loc)
{
// loc.getLatitude();
//loc.getLongitude();
latitude=loc.getLatitude();
longitude=loc.getLongitude();
}
#Override
public void onProviderDisabled(String provider)
{
//print "Currently GPS is Disabled";
}
#Override
public void onProviderEnabled(String provider)
{
//print "GPS got Enabled";
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.databasecollect"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<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:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
Related
I am building app that tracks distance traveled. At the moment, I am having trouble displaying the coordinates. The code runs, but the Distance, which I've set to display the coordinates when Start is clicked, displays 0.00 when clicked.
public class MainActivity extends Activity {
private Button historyButton, startButton, stopButton;
private TextView Distance;
double currentDistance;
private SensorManager sensorManager;
private Chronometer TimeRan;
private LocationManager mLocManager;
private LocationListener location;
double longitude, latitude;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing all buttons and textviews
historyButton = (Button) findViewById(R.id.History);
startButton = (Button) findViewById(R.id.Start);
stopButton = (Button) findViewById(R.id.Stop);
Distance = (TextView) findViewById(R.id.Distance);
TimeRan = (Chronometer) findViewById(R.id.Time_Spent);
mLocManager = (LocationManager) getSystemService(LOCATION_SERVICE);
location = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
currentDistance += .01;
Distance.append("/n " + location.getLongitude() + " " + location.getLatitude());
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
};
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode){
case 100:
configureButton();
break;
default:
break;
}
}
public void configureButton(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
,100);
}
return;
}
//Request update every minute or 16 meters
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//ignore permission
mLocManager.requestLocationUpdates("gps", 60000, 16, location);
}
});
};
public void GoToHistory(View v) {
Intent history = new Intent(this, History.class);
startActivity(history);
}
protected void EndWorkout(View v){
TimeRan.stop();
//ignore permission
mLocManager.removeUpdates(location);
}
}
Here is my Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.stuyk.runningapplication">
<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"/>
<uses-feature android:name="android.hardware.sensor.stepcounter"/>
<uses-feature android:name="android.hardware.sensor.stepdetector"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".History" />
</application>
</manifest>
locationManager.requestLocationUpdates(
provider,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
You should set MINIMUM_TIME_BETWEEN_UPDATES to time that you want it to be updated frequently and set MINIMUM_DISTANCE_CHANGE_FOR_UPDATES distance the update will change according to your location status.
I want to send the longitude and latitude from my onLocationChanged method inside the class GPSTracker to GPSPrincipal (Which is my main activity) and activate a toast that shows the longitude and latitude.
I'm having problems with the broadcastreciver to send the data from GPSTracker to GPSPrincipal, also, the aplication crashes and it shows the following error:
FATAL EXCEPTION: main java.lang.NullPointerException at
android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:344)
at
co.edu.javeriana.introcm.gpsexample.GPSTracker.onLocationChanged(GPSTracker.java:192)
The line 192 is:
sendBroadcast(broadcastIntent);
Last line on onLocationChanged inside GPSTracker
GPSPrincipal (Main activity):
public class GPSPrincipal extends Activity {
GPSTracker gps;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpsprincipal);
}
public void loca(View arg0) {
gps = new GPSTracker(GPSPrincipal.this);
if(gps.canGetLocation()){
TextView latitud = (TextView) findViewById(R.id.showLatitud);
TextView longitud = (TextView) findViewById(R.id.showLongitud);
latitud.setText(String.valueOf(gps.getLatitude()));
longitud.setText(String.valueOf(gps.getLongitude()));}
else {
gps.showSettingsAlert();}
}
public void cambio () {
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Located: - \nLat: " + intent.getExtras().get("latitude") + "\nLong: " + intent.getExtras().get("longitude"), Toast.LENGTH_LONG).show();}};
registerReceiver(receiver, new IntentFilter("com.example.broadcast.gps.location_change"));
} }
GPSTracker:
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
private Location location;
private double latitude; // latitud
private double longitude; // longitud
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context; //Obtener el contexto
getLocation();
}
public void onLocationChanged(Location location) {
Intent broadcastIntent = new Intent("com.example.broadcast.gps.location_change");
broadcastIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
broadcastIntent.putExtra("latitude", latitude);
broadcastIntent.putExtra("longitude", longitude);
sendBroadcast(broadcastIntent);
}
Manifest:
package="co.edu.javeriana.introcm.gpsexample"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".GPSPrincipal"
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>
Potentially seems to be that your context when calling sendBroadcast() is null within the ContextWrapper class. Perhaps you should try using mContext.sendBroadcast and see if that makes a difference.
I have a trouble. I can't get location on my HTC incredible.
It's my manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationfinder"
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"/>
<uses-permission android:name="android.permission.ACCESS_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>
</application>
It's my activity
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.NETWORK_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 current 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(), "WIFI Disabled", Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(), "WIFI Enabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText( getApplicationContext(), "snth", Toast.LENGTH_SHORT).show();
}
}/* End of Class MyLocationListener */
}
If I choose LocationManager.GPS_PROVIDER instead LocationManager.NETWORK_PROVIDER and GPS is off it currently makes toast with "GPS disabled". But if I use LocationManager.NETWORK_PROVIDER nothing happens no matter what the WiFi state is.
My application works as follows:
I have one location listener to every activity
My location listener(which contains location listener) send broadcast on location changed
All activities have broadcast receiver which allows them to set location on map
It has to be working like that, but even my service doesn't start. I'm exhausted writing this app so I place my code here, maybe I overlooked some simple issue. I need help with that topic:
GPS Service:
// package and imports
public class GeoService extends Service {
LocationListener GPSLocationListener, NetworkLocationListener;
LocationManager locationManager;
private IBinder mBinder;
#Override
public void onCreate() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
getPosition();
return Service.START_REDELIVER_INTENT;
}
#Override
public IBinder onBind(Intent intent) {
getPosition();
if (mBinder == null) mBinder = new GeoBinder();
return mBinder;
}
private void getPosition() {
GPSLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
sendPosition(location);
}
public void onProviderDisabled(String provider) {
Toast.makeText(getApplicationContext(), provider + " is now disabled. Turn it on to find better GPS position.", Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), provider.toUpperCase() + " is now enabled. Waiting for better position..", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case 0:
case 1:
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, NetworkLocationListener);
break;
case 2:
locationManager.removeUpdates(NetworkLocationListener);
break;
}
}
};
NetworkLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
sendPosition(location);
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, GPSLocationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, NetworkLocationListener);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) alert();
}
private void alert() {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage("Internet connection and GPS are not avaiable!.").setCancelable(false).setTitle("No location provider!").setIcon(android.R.drawable.ic_dialog_alert).setPositiveButton("Change settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
startActivity(new Intent(getApplicationContext(), SettingsActivity.class));
}
}).create().show();
}
void sendPosition(Location location) {
Intent gpsPosition = new Intent();
gpsPosition.putExtra("location", location);
gpsPosition.setAction(Context.LOCATION_SERVICE);
sendBroadcast(gpsPosition);
}
public GeoPoint getGeoPoint(final Location loc) {
int lat = (int) (loc.getLatitude() * 1e6);
int lon = (int) (loc.getLongitude() * 1e6);
return new GeoPoint(lat, lon);
}
public GeoPoint getLastLocation() {
GeoPoint lastKnownPoint;
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastKnownLocation == null) lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (lastKnownLocation != null) lastKnownPoint = getGeoPoint(lastKnownLocation);
else lastKnownPoint = new GeoPoint((int) (51.110582 * 1E6), (int) (17.031509 * 1E6));
return lastKnownPoint;
}
#Override
public void onDestroy() {
locationManager.removeUpdates(GPSLocationListener);
locationManager.removeUpdates(NetworkLocationListener);
}
public class GeoBinder extends Binder {
public GeoService getService() {
return GeoService.this;
}
}
}
One of Activities: (I tried to start or bind service but none method seems to be working).
// package and imports
public class ShareLocationActivity extends MapActivity {
private final int PICK_CONTACT = 123;
MapView.LayoutParams mapMarkerParams;
MapController mapController;
Location lastLocation;
ImageView mapMarker;
GeoPoint current;
MapView map;
private ServiceConnection mConnection;
GeoService mService;
boolean mBound = false;
BroadcastReceiver locationReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
setPosition((Location) intent.getParcelableExtra("location"));
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_location);
mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
GeoBinder binder = (GeoBinder) service;
mService = binder.getService();
mBound = true;
mapController = map.getController();
mapController.setCenter(mService.getLastLocation());
mapController.setZoom(17);
}
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
};
Intent intent = new Intent(this, GeoService.class);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
map = (MapView) findViewById(R.id.shareMapView);
map.setBuiltInZoomControls(true);
map.setSaveEnabled(true);
mapMarker = new ImageView(getApplicationContext());
mapMarker.setImageResource(android.R.drawable.presence_online);
Button share = (Button) findViewById(R.id.shareActivityButton);
share.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent contacts_intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contacts_intent, PICK_CONTACT);
}
});
IntentFilter filter = new IntentFilter();
filter.addAction(Context.ACTIVITY_SERVICE);
registerReceiver(locationReceiver, filter);
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
if (current != null) sendSms(data);
else Toast.makeText(getApplicationContext(), "No GPS data yet..", Toast.LENGTH_SHORT).show();
}
break;
}
}
#Override
protected void onResume() {
super.onResume();
if (!mBound) {
Intent intent = new Intent(this, GeoService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
};
#Override
protected void onPause() {
map.removeAllViews();
if (mBound) {
unbindService(mConnection);
mBound = false;
}
super.onPause();
}
#Override
protected void onDestroy() {
super.onDestroy();
map.removeAllViews();
}
void setPosition(Location location) {
map.removeAllViews();
current = mService.getGeoPoint(location);
mapMarkerParams = new MapView.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, current, MapView.LayoutParams.TOP_LEFT);
map.addView(mapMarker, mapMarkerParams);
mapController.setCenter(current);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
And to be sure Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gps.counter"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature android:name="android.hardware.location" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
<application
android:icon="#drawable/ico"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<uses-library android:name="com.google.android.maps" />
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SaveLocationActivity" />
<activity android:name=".ShareLocationActivity" />
<activity android:name=".RecordTrackActivity" />
<activity android:name=".SettingsActivity" />
<service
android:name=".GeoService"
android:enabled="true" />
</application>
</manifest>
The solution is to keep service and activity which uses it in one package.
It is small issue but can be big problem for someone.
I tried to implement the addProximityAlert method, but it does not fire. Here is my code:
SampleProximityAlert
public class SampleProximityAlert extends Activity {
/** Called when the activity is first created. */
protected LocationManager locationManager;
protected Location location;
public static Location pLocation;
protected Intent intent;
protected PendingIntent pIntent;
protected GeoPoint point;
public float distance;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
try {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 1000, 1,
new ProximityListener());
} catch (RuntimeException e) {
e.printStackTrace();
}
// point = new GeoPoint(49868004, 8626971);
try {
pLocation = new Location(location.getProvider());
} catch (RuntimeException e) {
e.printStackTrace();
}
Log.i("PROVIDER", location.getProvider());
pLocation.setLatitude(49.868004);
pLocation.setLongitude(8.626971);
distance = methodeDistance(pLocation);
Log.i("DISTANCE", String.valueOf(distance));
Log.i("POSITION",
String.valueOf(location.getLatitude()) + ","
+ String.valueOf(location.getLongitude()));
intent = new Intent(this, ProximityAlert.class);
intent.setAction("ProximityAlert");
pIntent = PendingIntent.getService(this, 0, intent, 0);
locationManager.addProximityAlert(49.868004, 8.626971, 429, 50000,
pIntent);
}
public float methodeDistance(Location mLocation) {
float mDistance = location.distanceTo(pLocation);
return mDistance;
}
}
ProximityAlert
public class ProximityAlert extends Service {
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i("Alert", "fired");
return null;
}
#Override
public void onCreate() {
super.onCreate();
Log.i("ONCREATE", "create ProximityAlert as Service");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("ONSTARTCOMMAND", "OnStartCommand");
return super.onStartCommand(intent, flags, startId);
}
}
ProximityListener
public class ProximityListener implements LocationListener {
String DEBUG_TAG = "ProximityListener";
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d(DEBUG_TAG, location.toString());
// tvD.setText(String.valueOf(location.distanceTo(pLocation)));
}
#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
}
}
XML Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tsystems.proximityAlert"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SampleProximityAlert"
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=".ProximityAlert">
<intent-filter>
<action android:name="ProximityAlert"/>
<category android:name="com.proaktiveOrtung"/>
</intent-filter>
</service>
<service android:name=".ProximityAlert"></service>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tsystems.proximityAlert"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SampleProximityAlert"
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=".ProximityAlert">
<intent-filter>
<action android:name="ProximityAlert"/>
<category android:name="com.proaktiveOrtung"/>
</intent-filter>
</service>
<service android:name=".ProximityAlert"></service>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
Sorry I selected not the complete code.
when you add a ProximityAlert you have to pass a PendingIntent to the LocationManager, these PendingIntent will be fired on a broadcastIntent and i dont see in your code when, where you register a receiver for that PendingIntent.
Now a question... why do you write a service if you add the ProximityAlert on an Activity?