2

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

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

     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);
    }
}   
4

2 回答 2

0

One solution would be to start an AsyncTask to get your Contact name. Then call startActivity() in the AsyncTask's onPostExecute() method, which runs back on the main UI thread. So in essence, you kick off an AsyncTask, let it get the contact name asynchronously, then call startActivity() only when it finishes.

Here's a nice tutorial: http://www.vogella.com/articles/AndroidPerformance/article.html

And the AsyncTask documentation: http://developer.android.com/reference/android/os/AsyncTask.html

Scroll down to "Using AsyncTask" in this guide: http://developer.android.com/guide/components/processes-and-threads.html#Threads

于 2012-10-31T20:26:45.713 回答
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中该类的更详细说明

于 2012-10-31T23:17:19.150 回答