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