Stepcounter in Phonegap - Background service - android

I am developing a Phonegap application which, among other things, has a step counter (which uses the accelerometer). Obviously, i need the app to count the steps even if the screen is locked. I have used a background service, acquired partial wake lock, but for some reason the service is still killed when i lock the screen. This is the code for the background service:
public class StepCounterService extends Service {
private ELStepCounter mStepCounter;
public static final String TAG = "STEP_COUNTER_SERVICE";
private void publishResult(int steps){
Intent intent = new Intent(TAG);
intent.putExtra("Steps", steps);
sendBroadcast(intent);
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
ELStepCounter.startInstance(StepCounterService.this);
mStepCounter = ELStepCounter.getsInstance();
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"StepCounterService");
wakeLock.acquire();
mStepCounter.setmStepDetectionListener(new StepCounterInterface() {
#Override
public void onStepDetected(final long steps) {
// TODO Auto-generated method stub
Toast.makeText(StepCounterService.this, steps + " steps", Toast.LENGTH_SHORT).show();
publishResult((int) steps);
}
#Override
public void error(int errorCode) {
// TODO Auto-generated method stub
}
});
return Service.START_STICKY;
}
}
Thanks in advance

I appreciate this answer comes very late. I am sending it just in case it is useful for future reference. I am not an expert on PhoneGap, however I have some experience with Android phones and I guess you may have been testing with one of them. In several Android phones the accelerometer is turned off when the screen is turned off. Not all of them. See a list of models at http://www.saltwebsites.com/2012/android-accelerometers-screen-off
I have asked a question on StackOverflow about that because it looks like Google Fit is kind of overcoming the limitation but I found no answer (see Step Counter in Android: always on?)
I hope it helps

Related

ANDROID : Reboot android device when my service killed/crashed

I have seen the default behaviour of many services, that they are restarted when killed by system or user.
Is it possible that if my service is killed or crashed the android device is rebooted instead of my service itself being restarted.
If so, then have android provided some mechanism that we can use to achieve the above.
First of all, it is really very bad pattern to reboot device on service destroy.
Anyways, you can achieve this using below code
public class demoService extends Service
{
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
}
#Override
public void onDestroy() {
super.onDestroy();
// this won't restart your phone instead it will ask for action
Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);
}
}

Always runs service in background on Android?

I am building an app which has a background service for continuously communicating with a Raspberry Pi.
I am making the service sticky(Service.START_STICKY) so that is will restart when it's killed by user or OS.
This works well for the first 30-40 minuten. After 30-40 minutes the services seems to be stopped.
Isn't there any way to avoid this problem? I know it's bad for the batterylife if there is service continiously runnnig in the background. The app is used in my own project to automate my room and is only used by me. So the batterylife isn't a problem.
public class RaspberryPiCommunication extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Do continuouswork here in a seperate thread
return Service.START_STICKY;
}
}
I also have another quetions about service:
If the service is started twice in the following example, will there be two instances of SomeObject() and is doSomWork() running twice in background?
public class RaspberryPiCommunication extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
SomeObject obj = new SomeObject();
obj.doSomeWork();
return Service.START_STICKY;
}
}
Are you running the service on its own thread ? By default, A service runs on the main thread. For a long running task like this run it in a separate thread
Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work.
Service will run only in one Instance. onStartCommand() is called every time you start the service.
http://developer.android.com/guide/components/services.html#StartingAService
Here is required piece of code.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
handler.postDelayed(runnable, 1000 );// 1000 - Milliseconds
if (appContext == null) {
appContext = getBaseContext();
}
Toast.makeText(appContext, "Services Started", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
Runnable runnable = new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
DoBackgroundTasks();
handler.postDelayed(this, 1000 ); // 1000 - Milliseconds
}
};
void DoBackgroundTasks()
{
//write code here.
}
In your manifest where you have declared your service, add this
android:process="remote"

Service in android 5.0+

I intended to work like this:
user switches on a feature: let say weather.
now weather data will come from server every 6 hours and will be shown to widget(remoteview), Now user switches off the feature. then widget should not show the weather or even refresh the data every 6 hours.
there are also 3-4 more features like that.
Now i had created a service to get all required data and than i have passed them to remoteview. For starting service i had used this in TimeOut Activity:
i = new Intent(TimeOut.this, TimeService.class);
i.setAction("com.example.Weather.Idle");
startService(i);
same for stopping service in switch off code:
stopService(i)
This code was working fine in API <=19. But in Lollipop it crashes at starting or stoping service.
I searched a lot in SO and also tried code for Binding or unbinding service but didn't help any.
Please help me with some code rather than just links...
Thanks in advance :)
Starting a service from any activity class
Intent intent = new Intent(MainActivity.this, BackgroundService.class);
startService(intent);
Here is service class code
public class BackgroundService extends Service{
public static Context appContext = null;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
if (appContext == null) {
appContext = getBaseContext();
}
Toast.makeText(appContext, "Services Started", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
Add your logic here. You can do some work here using a thread. You can stop service whenever you want and i hope you will not find any crash.
I have faced similar issue with Service in 5.0. This is probably not the correct answer, but it works. You could try. I use EventBus to communicate with my services. So when I want to stop the service I'd send,
EventBus.getDefault().post(new ServiceEvent(ServiceEvent.STOP_SERVICE));
In the service,
public void onEvent(ServiceEvent event) {
if (event.getEvent() == ServiceEvent.STOP_SERVICE) {
methodToStopService();
}
}
private void methodToStopService() {
// do some stuff
stopSelf();
}
Make sure you register your service for events.
private void registerEventBus() {
EventBus eventBus = EventBus.getDefault();
if (!eventBus.isRegistered(this)) {
eventBus.register(this);
}
}
ServiceEvent class - It's my own class which I use with EventBus.
public class ServiceEvent {
private int event;
public static final int STOP_SERVICE = -1;
public ServiceEvent(int event) {
this.event = event;
}
public int getEvent() {
return event;
}
}

Android service not working

I'm trying to call a service class to update the value of a variable from my widget but it doesn't ever seem to get to the service class. I've had a look at some examples and I can't figure out what I'm doing wrong, and I don't really know very much about services yet. All help appreciated.
Service class
public class toggleMonitoringService extends Service{
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate()
{
Log.d("Me","creating service");
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int startId, int something) {
// TODO Auto-generated method stub
String toggle = intent.getExtras().getString("Toggle");
Log.d("Me","Toggle : " + toggle);
if (toggle.equals("app1"))
{
UpdateWidgetService.monitorApp1 = !UpdateWidgetService.monitorApp1;
}
else if (toggle.equals("app2"))
{
UpdateWidgetService.monitorApp2 = !UpdateWidgetService.monitorApp2;
}
super.onStartCommand(intent, startId, something);
return 0;
}
}
Where I set up the intent and pending intent to handle the button click from the widget
Intent monitor1toggle = new Intent(this.getApplicationContext(),toggleMonitoringService.class);
monitor1toggle.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
monitor1toggle.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
monitor1toggle.putExtra("Toggle","app1");
PendingIntent monitor1 = PendingIntent.getService(getApplicationContext(), 0 , monitor1toggle,0);
remoteViews.setOnClickPendingIntent(R.id.firstappstatus, monitor1);
Try start service manually, wihtout using PendingIntent.
Better way is not to start service each time you need to do something, but to start it once, bind to it and use common method calls when you need something from the service.
For your example even a simple Thread would be more appropriate.

Android service keeps running indefinitely

I'm trying to create a service in android which i want to keep running in background indefinitely. So I tried to create one like :
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, mssg, Toast.LENGTH_SHORT).show();
}
public int onStartCommand(Intent intent, int flags,int startid) {
Toast.makeText(this, mssg, Toast.LENGTH_LONG).show();
Log.d("Start:", "Service running");
// my code here
return START_STICKY;
}
But when I run this code, the toast messages and logs are only shown once , so does it mean service runs only first time. If it is running again n again which API of it is being called repeatedly ?
Thanks,
shadow.
Running a Service indefinitely and running the same piece of code again and again are two very different things. The piece of code you provided will allow the service to be running the background as long as the Android system doesn't decide to stop it.
Not sure what functionality you are looking to implement, but i suggest you read up on what exactly a Service is used for and what your requirements are.
i suggest you change return START_STICKY;
with return START_NOT_STICKY;
as details given in documentation here.

Categories

Resources