-1

I'm developing an app that calculates an algorithm at the background. Since the application starts, till it ends. This is a tracking algorithm (with now further explanation about the algorithm operation principles).

So the background task need to be calculated at all screens on the app regardless of user actions on app, clicks, wifi communication messages (already done on app) , everything needs to be done while the algorithm is running at the background.

Is it an AsyncTask ? If not what else? The application is running and the algorithm is being calculated at a specific screen now, i want to make it a background process with no respect to current application screen.

An example will be appreciated

P.S- Further developing , do not need to be discussed if not needed right now: 1. The next stage is to insert an indication (virtual bulb) that change between to states depends on algorithm result each time. 2. The algorithm is getting data from USB device attached to the phone as the phone is the host using FTDI chip.

Digol
  • 390
  • 1
  • 15

3 Answers3

0

It's clearly a Service that you need - that's exactly their main purpose - to do long-running operations in the background, no matter what Activity the user is playing with in the foreground. You can read more about Services HERE.

Vesko
  • 3,750
  • 2
  • 23
  • 29
0

Use broadcast reveicer

You need to define a receiver in manifest with action name android.intent.action.BOOT_COMPLETED.

<!-- Start the Service if applicable on boot -->
<receiver android:name="com.prac.test.ServiceStarter">
<intent-filter>
    <action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>

Make sure also to include the completed boot permission

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Use Service for this to make anything persist. And use receivers to receive Boot Up events to restart the service again if system boots..

Code for Starting Service on boot up. Make Service do your work of checking sms or whatever you want. You need to do your work in MyPersistingService define it your self.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ServiceStarter extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent("com.prac.test.MyPersistingService");
        i.setClass(context, MyPersistingService.class);
        context.startService(i);
    }
}
Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20
  • I have a [link](http://www.learn-android-easily.com/2013/07/bootcompleted-broadcastreceiver-in.html) for you.Thats what i can do max.Accept the answer if it helped.for other users. – Neal Ahluvalia Apr 22 '15 at 10:14
  • thanks Neal! did it, with services, posted new Question on forum, will be appreciate if you check it also. you can see how i did the Service there! – Digol Apr 26 '15 at 01:27
0

It's clear that you need execute your algorithm in a independient thread. You must choose how you want do this.

My recommendation is a Service or a IntentService.

https://developer.android.com/training/run-background-service/index.html

You can get a fantastic example here, with a Service which is running in the background and a TimerTask executed each a period of time.

App to monitor other apps on android

Community
  • 1
  • 1
juanhl
  • 1,170
  • 2
  • 11
  • 16