-2

I want to develop a voice application which has to respond when the user speaks some commands. I think I need a service to accomplish my task.But I have learnt from several resources that a service cannot run for a long time.The android system automatically destroys a service if it sits idle.

So my question is whether it is possible to run a service continuously without being destroyed and respond to user actions?

I am new to Android development.So if there is anything wrong in the question, please correct me.

Thanks for the help!

raman kilambi
  • 51
  • 1
  • 1
  • 3
  • If want to respond speak command then create broadcast receiver that catch that event and run your service class. – Divyesh Patel Nov 09 '16 at 13:24
  • 1
    Android kills a service whenever it needs resources for more important tasks and you can't prevent it from doing that. What you can do is to return START_STICKY in your onStartCommand() inside your service class so your service will start again after each time gets killed by the system. – Erfan GLMPR Nov 09 '16 at 13:28
  • Check out this library https://github.com/cmusphinx/pocketsphinx – Veneet Reddy Nov 09 '16 at 13:58

1 Answers1

0

You can use startForeground.

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
        System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
        getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);

They are killed only as a last resort—if memory is so low that they cannot all continue to run. https://developer.android.com/guide/components/processes-and-threads.html

1st priority.

T.S
  • 911
  • 8
  • 24