يتسبب نشاط التعرف على صوت Android الذي يسمى في طريقة "oncreate" إلى تحميل التطبيق ببطء

StackOverflow https://stackoverflow.com/questions/3841381

سؤال

في تطبيق Android الخاص بي ، أسمي التعرف على الصوت في طريقة OnCreate الخاصة بي لنشاط بدء التشغيل الخاص بي. لقد جعلت الأمر تفضيلًا للبدء بالتحكم الصوتي أم لا. ومع ذلك ، يستغرق التطبيق حوالي 5-7 ثانية للتحميل عند تشغيل التعرف على الصوت. عندما يتم إيقاف تشغيله ، يبدأ التطبيق على الفور تقريبًا. فيما يلي نموذج رمز ، لقد أضفت Free_form و Max_Results 1 ومطالبة مخصصة لي.

Intent intent = new Intent("android.speech.action.RECOGNIZE_SPEECH");  
startActivityForResult(intent, 0);

حقا لدي سؤالان:

أليست نشاط البداية (النية) يتم تشغيله في موضوع منفصل؟

لماذا استدعاء التعرف على خطاب Android العادي يستغرق وقتًا طويلاً لتحميله في طريقة OnCreate الخاصة بي؟

هل كانت مفيدة؟

المحلول

ربما لا تنتظر لإكمال هذه العملية من OnCreate ، بدلاً من ذلك قم بتشغيلها بحيث تعود النتيجة إلى معالج رد الاتصال ، أو قم بإنشاء سلسلة رسائل وتطلقها من هناك.

نصائح أخرى

لا يستغرق الإعداد للتعرف على الصوت 5-7 ثوانٍ على Motorola Droid ، فهو <0.5 ثانية ، لذلك قد يكون لديك مشكلة أخرى في الكود الخاص بك. لا شيء أقل ، إليك رمز لنقل التحضير من onCreate إلى موضوع خلفية:

public class VoiceRecognition extends Activity implements OnClickListener {

private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
ImageButton speechButton;
static List<ResolveInfo> systemActivities=null;
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.voice_recognition);

    // Get display items for later interaction
    speechButton = (ImageButton) findViewById(R.id.btn_speak);
    mList = (ListView) findViewById(android.R.id.list);
    // Asynchronously activate speech recognition
    new getSpeechTask().execute();
}
private class getSpeechTask extends AsyncTask<Void,Void,List<ResolveInfo>>{
    protected List<ResolveInfo> doInBackground(Void...params) {
        List<ResolveInfo> sysAct=null;
        if(sysAct==null){
            PackageManager pm = getPackageManager();
            sysAct = pm.queryIntentActivities(
                    new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0
            );
        }
        return sysAct;
    }//doInBackground
    protected void onPostExecute(List<ResolveInfo> result) {
        systemActivities=result;    
        setSpeechStatus();
    }//apply result
}//getSpeechTask    
void setSpeechStatus(){
    if(systemActivities.size()>0){
        speechButton.setOnClickListener(
            new OnClickListener(){
                @Override
                public void onClick(View v) {
                    startVoiceRecognitionActivity();
                }
            }
        );
        speechButton.setEnabled(true);  
    }else{
        speechButton.setEnabled(false); 
    }
}
void startVoiceRecognitionActivity(){
    if(systemActivities==null){
        PackageManager pm = getPackageManager();
        systemActivities = pm.queryIntentActivities(
                new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
        setSpeechStatus();
    }
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Missive(c) Speech Input");
    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}

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


/**
 * 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);
}
}

مع Layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
    />
   <ImageButton android:id="@+id/btn_speak"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@android:drawable/ic_btn_speak_now"
   />
</LinearLayout>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top