I'm trying to run a service in the background that sends texts messages of the location of the phone. In the service I use a Timer. The code that sends the messages runs correctly but when I try to put the code in to find the users location I always run into errors.
I tried to call a new thread and used a looper but now I'm getting a nullpointerexception which points to these two lines of code:
sendSmsMessage();
and within this method:
if (coordinates.equals(null) || coordinates.equals("") || coordinates == null || coordinates == ""){
coordinates = "Could Not Receive Location";
}
So, the question I'm asking is why is this error occurring? If you could help me out it would be greatly appreciated. And if I'm just overlooking something very simple please point it out. Thanks!
Here's the code:
public class MessageService extends Service{
int counter = 0;
private Timer timer = new Timer();
public String textTime, phoneNumber;
public int updateInterval;
int lat, lng;
String coordinates, latitude, longitude;
LocationManager locationManager;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
//receives the intent extras from the calling intent
textTime = intent.getStringExtra("textTime");
phoneNumber = intent.getStringExtra("phone");
phoneNumber = "5556";
//the following if statement has to do with transferring the string textTime into a number that can be used
if (textTime.equals("15 Minutes")) {
updateInterval = (15 * (60000));
}else if (textTime.equals("30 Minutes")) {
updateInterval = (30 * (60000));
}else if (textTime.equals("1 Hour")){
updateInterval = (60 * (60000));
}else {
updateInterval = (15 * (60000));
}
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
new Thread(){
public void run(){
Looper.prepare();
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
//makeUseOfNewLocation(location);
lat = (int) (location.getLatitude() * 1E6);
lng = (int) (location.getLongitude() * 1E6);
latitude = Integer.toString(lat);
longitude = Integer.toString(lng);
coordinates = "Coordinates: " + latitude + ", " + longitude + ". Latitude: " + latitude + " Longitude: " + longitude + ". Respond 'END' to stop texts.";
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
Looper.loop();
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}else{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
}.start();
//the following method should use a timer to send a sms message in a timed interval. It also should implement using a different thread
doSomethingRepeatedly();
return START_STICKY;
}
public void doSomethingRepeatedly(){
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
//the following code should be what is done repeatedly
//Log.d("MessageService", String.valueOf(++counter));
sendSmsMessage();
}
}, 0, updateInterval);
}
//the following code handles sending the text message
public void sendSmsMessage(){
SmsManager sms = SmsManager.getDefault();
if (coordinates.equals(null) || coordinates.equals("") || coordinates == null || coordinates == ""){
coordinates = "Could Not Receive Location";
}
sms.sendTextMessage(phoneNumber, null, "test" , null, null);
}
public void onDestroy() {
super.onDestroy();
if (timer != null) {
timer.cancel();
}
}
}//end of service
And here is the logcat output:
10-19 20:23:28.096: E/AndroidRuntime(944): FATAL EXCEPTION: Timer-0
10-19 20:23:28.096: E/AndroidRuntime(944): java.lang.NullPointerException
10-19 20:23:28.096: E/AndroidRuntime(944): at com.app.MessageService.sendSmsMessage(MessageService.java:113)
10-19 20:23:28.096: E/AndroidRuntime(944): at com.app.MessageService$2.run(MessageService.java:105)
10-19 20:23:28.096: E/AndroidRuntime(944): at java.util.Timer$TimerImpl.run(Timer.java:284)