Background Service Getting My Phone Heated Up - android

I have a code which runs a service that get's user location update, the service is running fine.. however my phone is heating up for just after minutes of the service running.
#Override
public int onStartCommand(Intent intent, int flags, int startId){
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
TrackLocation tl = new TrackLocation();
if(LocationManager.GPS_PROVIDER != null){
Toast.makeText(this, "service started...", Toast.LENGTH_LONG).show();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 600000, 150, tl);
}else{
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 600000, 150, tl);
}
return START_STICKY;
}
that is how I start the service, I don't know what's wrong I'm newbie at android dev. Please help! :(

Related

How can I run a service with regular location updates in android

I'm using LocationListener in my service to get regular location updates. When I close the process, the service does not give any updates to me. What can I do?
you service as a START_STICKY.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// initialize location
initLocation();
Log.e("", "onStartCommand");
return START_STICKY;
}
It will run in background after kill..and if there is any update of location you can get update.

best way to have a background service making server communication in android application

Currently i am developing a android application that monitors some user behaviours, those behavious are location, accelerometer, pictures, sms and mms information.
I am facing a daunting problem. I am reading information from these services with a service that has a alarm manager.
The alarm manager is has follow:
#Override
public void onCreate() {
Log.e(TAG, "onCreate");
Intent intent = new Intent(this, WorkingService.class);
PendingIntent pintent = PendingIntent.getService(
getApplicationContext(), 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
calendar.setTimeInMillis(System.currentTimeMillis());
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
15000, pintent);
}
this will call the onstartcommand of this class every 15 seconds.
#SuppressWarnings("static-access")
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
and in the onStartCommand of this service i read stuff from the services that have location updates and other information.
The problem is that this is very costly in terms of power. I was told that the amount of services doesn't have a significant impact on the battery but accessing those services via alarm manager does. What is the alternatives i have in this problem? and can you confirm that the thing that is consuming more battery in my app is in fact the alarm manager service.
And also if it is, what would be a better way to periodically read from the services and send that data into my web services?
Why not try using a thread? Something like this would work:
Thread t = new Thread()
{
#Override
public void run()
{
while (true)
{
try
{
Thread.sleep(100);
} catch (InterruptedException ie)
{
fThreadRunning = false;
ie.printStackTrace();
}
}
}
};
t.start();
It's obvious that the more you read the location the more you will drain your battery, I would recommend you looking into Android DDMS to see what threads are causing the biggest battery drain within your application.

Android: LocationManager service stops updating when set to NETWORK_PROVIDER

Right, the situation is as follows;
We have developed an app that checks the location of the user every 5-10 minutes for location specific content. To do so, I've created a Service to stick to the background (so it can update even when the app isn't directly in the foreground) in which a LocationController is created to check the latest known location. When it's finished, it cleans up and the location is sent to a database (which is a necessity for this concept).
This all works fine, as long as I check the location with GPS_PROVIDER. However, when I switch it around to NETWORK_PROVIDER, it may check the location once more before dying completely.
I've tried multiple options to prevent this problem, but nothing seems to be working with the update service when I swap that 1 setting.
Here are a few snippets which should be relevant to the service:
UpdateService:
#Override
public void onCreate() {
super.onCreate();
Log.d("Debug", "Created service");
PowerManager mgr = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
wakeLock.acquire();
IntentFilter filter = new IntentFilter();
filter.addAction(UPDATE_ACTION);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Starting", "Starting Service");
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(UPDATE_ACTION);
alarmManagerPendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
alarmManagerPendingIntent.cancel();
mAlarmManager.cancel(alarmManagerPendingIntent);
mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
alarmManagerPendingIntent);
mLocationController = new LocationController(this, this);
updateHandler = new Handler();
return START_STICKY;
}
LocationController:
private LocationListener locationListener = new LocationListener() {
private boolean didSendLocation;
public void onLocationChanged(Location location) {
mLastLocation = location;
mCallback.locationUpdate(location,false);
didSendLocation = true;
}
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Debug", "Status changed: "+status);
Location _lastLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("Debug", "Last location: "+_lastLocation);
if(!didSendLocation)
{
mCallback.locationUpdate(_lastLocation,false);
}
didSendLocation = false;
}
public void onProviderEnabled(String provider) {
Log.d("Debug", "Provider Enabled");
}
public void onProviderDisabled(String provider) {
Log.d("Debug", "Provider disabled");
}
};
public LocationController(Context mContext, LocationControllerCallback callback) {
this.mContext = mContext;
mCallback = callback;
Log.d("Debug", "Created LocationController");
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 100, locationListener);
mLastLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mCallback.locationUpdate(mLastLocation,true);
}
So, the code works as it is now, but when I swap:
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 100, locationListener);
with
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300000, 100, locationListener);
It will no longer update. Any ideas?
When you use this to get location updates:
mLocationManager.requestLocationUpdates(...)
you don't need AlarmManager methods as the first parameter of the requestLocationUpdates method already acts as a "timer". See here for more info.
Also, you may need to include the following directives in your AndroidManifest.xml file:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>
Without these permissions, the requestLocationUpdates method may not work as you expected regardless of which provider you use.
UPDATE 1:
I would have your LocationController class initialiased in a Service, for example:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
this.mLocationController = new LocationController(this);
return Service.START_NOT_STICKY;
}
Then, start the above Service in the onResume method of an activity (MainActivity or another activity in your project), and pause the Service in the onPause method of the same activity.
If its specific to the NETWORK_PROVIDER, you may be seeing this bug:
https://code.google.com/p/android/issues/detail?id=57707
Original bug report included the Samsung Galaxy S3 as an affected device.
To help troubleshoot - you may also want to try selecting the NETWORK_PROVIDER in the GPS Benchmark app (full disclosure - its my app) to see if this app exhibits the same behavior. If so, its likely an issue with the device.
If you determine you're seeing the above issue, please be sure to star it on the issue page if you'd like to see it fixed.
I've solved the problem by switching to the Google Play Fused Location provider. A more detailed post around this can be found here: https://stackoverflow.com/a/19282976/3532181
Sadly the standard network provider just seemed too unreliable when it came to updates, ranging from an update within 8 minutes to hours before a new update was sent to the app.
I had a similar issue. I was able to fix this by adding the required permissions. The issue is related to new background service limitations.
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
Check this documentation for better understanding.

Android GPS not working in service

Im trying to get my GPS working in a service but its not working. The GPS bit works on its own but not in the service.
I have tried debugging with System.out.println() and fond where it stops working but cant work out why it all looks good.
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("test 1");
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
System.out.println("test 2");
LocationListener lli = new myLocationListener();
System.out.println("test 3");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, lli);
System.out.println("test 4");
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
class myLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if(location != null){
pet.setMeter();
}
}
It gets to Test 4 then dies. I am lost at why so if anyone can help that would be awesome thanks.
A nice gps tracker guide: http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/
Just needed to add this.mLocation = location in onLocationChanged
He also wraps it into a service.

Android service's new thread error: Thread already started

There's broadcastReceiver receive boot message and set a alarmmanager.
It start a service every DELAY mins:
Intent intent = new Intent();
intent.setAction("org.jxdwinter.getMessageServcie");
PendingIntent pendingintent = PendingIntent.getService(arg0, 0, intent, 0);
am = (AlarmManager) arg0.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), DELAY, pendingintent);
The service poll data from romate server in a new thread:
public int onStartCommand(Intent intent, int flags, int startId)
{
this.getMessage.start();
return Service.START_NOT_STICKY;
}
private class GetMessage extends Thread
{
public void run()
{
... ....
}
}
And when the app started, I can get the data at very first time and there's no
error.
Then the service start again in DELAY mins , there's an error:Thread already started.
Why is happened? Pls help me with this. Thank you!
Maybe you could just create a new instance of your thread each time you need it to run:
public int onStartCommand(Intent intent, int flags, int startId)
{
new GetMessage().start();
return Service.START_NOT_STICKY;
}
Is instance of GetMesdsage class you are using by any chance static? If yes, do not doe this - initialize new one

Categories

Resources