需要应用程序等待进程完成然后继续

时间:2012-10-31 19:50:20

标签: android for-loop sms wait

我在这里已经阅读了很多关于这个问题的帖子,但我似乎无法让我的代码等到它继续之前找到联系人名称并加载我的下一个活动。如果它是一个短的短信它工作正常,但如果它是一个特别长的短信它崩溃。请任何帮助。

这是下面建议的新代码问题仍然相同

     new LoaderAsyncTask().execute();

    }

public class LoaderAsyncTask extends AsyncTask<Void, Void, Void> {
    // Variables to pass data between doInBackground() and onPostExevute() here

    protected Void doInBackground(Void... params) {

         Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(smsSender));
                Cursor cursor = ((Context) contexts).getContentResolver().query(uri,
                    new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
                    null, null, null);
                contactId = "";
                name="unknown";
                if (cursor.moveToFirst()) {
                    do {
                    contactId = cursor.getString(cursor.getColumnIndex(PhoneLookup._ID));
                    name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));

                    } while (cursor.moveToNext());
                }


        return null;
    }

    protected void onPostExecute(Void result) {
         ////////////////////////////////////
        // start a new task before dying

        intents.setClass((Context) contexts, SendSMSActivity.class);
        intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // pass Serializable object
        intents.putExtra("PhoneNumber", smsSender);
        intents.putExtra("smsBody", smsBody);
        intents.putExtra("SmsMessageId", SmsMessageId);
        intents.putExtra("contactId", contactId);
        intents.putExtra("SenderName", name);
        // start UI
        ((Context) contexts).startActivity(intents);
    }
}   

2 个答案:

答案 0 :(得分:0)

一种解决方案是启动AsyncTask以获取您的联系人姓名。然后在AsyncTask的startActivity()方法中调用onPostExecute(),该方法在主UI线程上运行。所以从本质上讲,你启动AsyncTask,让它以异步方式获取联系人名称,然后只有在完成时才调用startActivity()

这是一个很好的教程: http://www.vogella.com/articles/AndroidPerformance/article.html

AsyncTask文档: http://developer.android.com/reference/android/os/AsyncTask.html

向下滚动到本指南中的“使用AsyncTask”: http://developer.android.com/guide/components/processes-and-threads.html#Threads

答案 1 :(得分:0)

使用类似的东西:

public class ActivityA extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {

        new LoaderAsyncTask().execute();
    }

}

public class LoaderAsyncTask extends AsyncTask<Void, Void, Void> {
    // Variables to pass data between doInBackground() and onPostExevute() here

    @Override
    protected Integer doInBackground(Void... params) {

        // yoyr loader stuff here

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // update UI or call ActivityB here

         ////////////////////////////////////
        // start a new task before dying
        Intent = ... // not shown in your code
        intent.setClass(context, SendSMSActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // pass Serializable object
        intent.putExtra("PhoneNumber", smsSender);
        intent.putExtra("smsBody", smsBody);
        intent.putExtra("SmsMessageId", SmsMessageId);
        intent.putExtra("contactId", contactId);
        intent.putExtra("SenderName", name);
        // start UI
        context.startActivity(intent);
    }
}

检查AsyncTask文档,了解http://developer.android.com/reference/android/os/AsyncTask.html

中有关该类的更详细说明