Change activity to service.

Put your problem here if it does not fit any of the other categories.

Change activity to service.

Postby tanth90 » Wed Mar 14, 2012 3:14 am

I'm using Google Api speech to recognition voice.
I try to built a simple code to use it. ( I find out code on stackoverflow website, but the class for recognition is an Activity).
Now, I want to change it into a Service.
But I don't know how to do that.
Can someone help me?
Thank so much!
Code: Select all
[syntax="java"]
public class VoiceRecognitionActivity extends Activity implements OnClickListener{
   private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;

    private ListView mList;

    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Inflate our UI from its XML layout description.
        setContentView(R.layout.main);

        // Get display items for later interaction
        Button speakButton = (Button) findViewById(R.id.btn_speak);

        mList = (ListView) findViewById(R.id.list);

        // Check to see if a recognition activity is present
        PackageManager pm = getPackageManager();
        List<ResolveInfo> activities = pm.queryIntentActivities(
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        if (activities.size() != 0) {
            speakButton.setOnClickListener(this);
        } else {
            speakButton.setEnabled(false);
            speakButton.setText("Recognizer not present");
        }
    }

    /**
     * Handle the click on the start recognition button.
     */
    public void onClick(View v) {
        if (v.getId() == R.id.btn_speak) {
            startVoiceRecognitionActivity();
        }
    }

    /**
     * Fire an intent to start the speech recognition activity.
     */
    private void startVoiceRecognitionActivity() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
        startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
    }

    /**
     * Handle the results from the recognition activity.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
            // Fill the list view with the strings the recognizer thought it could have heard
            ArrayList<String> matches = data.getStringArrayListExtra(
                    RecognizerIntent.EXTRA_RESULTS);
            mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                    matches));
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}
[/syntax]
tanth90
Freshman
Freshman
 
Posts: 4
Joined: Wed Mar 14, 2012 3:04 am

Top

Re: Change activity to service.

Postby nuetronprotron » Wed Mar 14, 2012 6:12 am

Hi tanth90,
I am sorry that i did not understand your requirement logically,Why would you want to change this activity class into service.Are you trying to use it as google voice search command feature,because you need the hardware interaction with the user and user should know what is he trying to do.
nuetronprotron
Developer
Developer
 
Posts: 37
Joined: Tue Jan 10, 2012 10:22 am

Re: Change activity to service.

Postby tanth90 » Thu Mar 15, 2012 10:46 am

I'm trying to write dictionary for blind people that they will interact with dict. by voice. I want my recognition will be hidden in background to listen what they say and transfer word to activity: dictionary. but now i can't change my Recognition class from activity to service.
Someone can help me?
tanth90
Freshman
Freshman
 
Posts: 4
Joined: Wed Mar 14, 2012 3:04 am

Re: Change activity to service.

Postby Phyll » Fri Mar 16, 2012 4:30 pm

Hi tanth,

That particular api is not really intended for use in a service. Your problem will be how to make the startactvityforresult call. It is primarily intended to be added to an app and used as in the example you show.

Another consideration is the button. Services don't have them so you would have to signal your service somehow. I just don't see how moving this into a service benefits anything. But you might try passing your service a context and using that to send the intent. Of course after that, the response would have to be caught by the app anyway.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Change activity to service.

Postby tanth90 » Mon Mar 19, 2012 2:16 am

Thank you Phyll. I'll try.
tanth90
Freshman
Freshman
 
Posts: 4
Joined: Wed Mar 14, 2012 3:04 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSN [Bot] and 30 guests