我在这里阅读了很多关于这个问题的帖子,但我似乎无法让我的代码等到它找到联系人姓名后再继续并加载我的下一个活动。如果是短短信,它可以正常工作,但如果是特别长的短信,它会崩溃。请任何帮助。
这是下面建议的新代码问题仍然相同
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);
}
}