2

I am trying to create a service(in smart watch) which should run forever even if the user is not using the app. Main purpose is to use the service for Speech Recognition. So whenever user say a specific word the app should be able to respond just like google voice.

However, for test purpose, I am printing an integer in the Log, but it only executed once. that means the service is not running forever.

My service code:

public class VoiceService extends Service {

private IBinder mBinder = new VoiceBinder();
int i = 1;
public VoiceService() {
}

@Override
public void onCreate() {
    super.onCreate();
    updateLog();
}

public void updateLog() {
    ++i;
    Log.v("DATA", Integer.toString(i));
}
@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    return mBinder;
}


//inner helper class
public class VoiceBinder extends Binder {

    //constructor
    VoiceService getService() {
        return VoiceService.this;
    }
}
}

My MainActivity Code:

serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            VoiceService.VoiceBinder binder = (VoiceService.VoiceBinder) iBinder;
            iBinder = (IBinder) binder.getService();
            MainActivity.this.voiceService = ((VoiceService.VoiceBinder)iBinder).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            voiceService = null;
        }
    };

    //bind service
    bindService(new Intent(this, VoiceService.class), serviceConnection, getApplicationContext().BIND_AUTO_CREATE);
    startService(new Intent(this, VoiceService.class));

AndroidManifest:

<service
        android:name=".VoiceService"
        android:enabled="true"
        android:exported="false">

    </service>

My output:

12-20 22:27:20.307 23624-23624/? V/DATA: 2
Nix
  • 142
  • 3
  • 13

1 Answers1

2

You might want to go through Running in a Background Service wherein it mentions that

Long-running foreground operations can cause problems and interfere with the responsiveness of your user interface, which annoys your users and can even cause system errors. To avoid this, the Android framework offers several classes that help you off-load operations onto a separate thread that runs in the background. The most useful of these is IntentService.

And, also note that:

If your app targets Android 5.0 (API level 21), you should use JobScheduler to execute background services.

For best application's performance and minimize draining of battery, you may want to also check the following links:

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22
  • thanks for the reply. I will do that. Currently I a getting error. I have somehow managed to execute my code continuously via CountDownTimer, but after a certain period, garbage collector frees the memory and my main task does not execute though the CountDownTimer keeps on Executing the method. – Nix Dec 21 '16 at 12:29