应用程序中使用的AsyncTask仍然占用UI线程? [英] App using AsyncTask still hogging the UI thread?
问题描述
我已经写了通过共享菜单坐在(快速传送电子邮件给自己链接到的事情,我在RSS阅读器在Web上查找或看)就我使用的是intent.action.SEND一个应用程序意图过滤器:
I've written an app that sits in the 'Share via' menu (for quickly emailing myself links to things I find on the web or look at in RSS readers) For this I'm using an intent.action.SEND intent-filter:
<activity
android:name="uk.co.baroquedub.checkit.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
下面是在MainActivity包时,它将从意向的页面标题和网址,并使用单独的GMailSender类直接发邮件给我这个信息:
Here's the MainActivity package, it grabs the page title and url from the Intent and uses a separate GMailSender class to directly email me this info:
package uk.co.baroquedub.checkit;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private static Dialog dialog;
String title;
String url;
String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
String action = intent.getAction();
// if this is from the share menu
if (Intent.ACTION_SEND.equals(action)) {
title = intent.getStringExtra(Intent.EXTRA_SUBJECT);
url = intent.getStringExtra(Intent.EXTRA_TEXT);
// Flipboard fix (remove title in URL)
url = url.replace(title, "");
if (url != null){
url = title+"\n"+url;
} else {
url = "error getting URL";
}
// Asynch Task
doSendTask task = new doSendTask();
task.execute(new String[] { url });
}
}
protected void showDialog (String response){
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle(response);
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
finish();
}
});
dialog.show();
}
private class doSendTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
String senderPassword = getResources().getString(R.string.senderPassword);
String senderEmail = getResources().getString(R.string.senderEmail);
String recipientEmail = getResources().getString(R.string.recipientEmail);
String subjectText = getResources().getString(R.string.subjectText);
GMailSender sender = new GMailSender(senderEmail, senderPassword);
try {
sender.sendMail(subjectText,
url,
senderEmail,
recipientEmail);
response = "Email sent";
} catch (Exception e) {
//Log.e("SendMail", e.getMessage(), e);
response = "Error sending email";
}
return response;
}
@Override
protected void onPostExecute(String result) {
showDialog(result);
}
}
@Override
public void onDestroy() {
super.onDestroy();
/*
* Kill application when the root activity is killed.
*/
UIHelper.killApp(true);
}
}
1版工作得很好,但我从内'的onCreate这意味着,直到通过电子邮件发送通知出现了,手机浏览器将不响应发送电子邮件(我是不是能够滚动或导航到一个新页面)。然后,我改变了code(根据以上)放置电子邮件发送code的AsyncTask的类中 - 不过虽然应用仍然有效浏览器仍没有响应,直到出现的对话框。 AsyncTask的似乎并没有区别。
Version 1 worked fine but I was sending the email from within 'onCreate' which meant that until the "Email sent" notification appeared, the phone's browser would be unresponsive (I wasn't able to scroll or navigate to a new page). I then changed the code (as per above) to place the email sending code inside an AsyncTask class - but although the app still works the browser remains unresponsive until the dialog appears. AsyncTask seems to have made no difference.
任何人都可以解释为什么,并希望提出一个解决方案?
Can anyone explain why, and hopefully suggest a solution?
推荐答案
?回答 ...我已经大规模简化上述code,试图找出什么可能是错误的。我创建了一个沼泽标准应用和使用下列内容:
ANSWER?... I've massively simplified the above code to try to work out what might be wrong. I created a bog standard App and used the following:
package uk.co.baroquedub.testcheck;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doSendTask task = new doSendTask();
task.execute(new String[] { "urlString" });
}
protected void showDialog (String response){
Toast.makeText(this, response, Toast.LENGTH_SHORT).show();
finish();
}
private class doSendTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
try {
Thread.sleep(5000);
response = "Waited";
}
catch (InterruptedException ex) { }
return response;
}
@Override
protected void onPostExecute(String result) {
showDialog(result);
}
}
}
这已经让我看到了什么错:我的应用程序打开浏览器的顶部(会出现一个白色的屏幕显示应用程序的名称的标题栏)我是不是我的正确应用意识到这一点(以上),因为我是用了用透明背景的一个主题。
This has allowed me to see what's going wrong: my app is opening on top of the browser (a white screen appears with a title bar showing the name of the app) I wasn't aware of this with my proper app (above) because I was using a Theme that used a transparent background.
请参阅:对问题的演示截屏
See: screencast for demo of problem
因此,尽管该电子邮件被发送作为AsyncTask的,而这种情况正在发生本身是出现在浏览器顶部的应用程序 - 这正是由被访问停止它。 (我会后对这个请求帮助作为一个单独的问题)
So although the email is being sent as an AsyncTask, while this is happening the app itself is appearing on top of the browser - which is what is stopping it from being accessible. (I'll post a request for help on this as a separate question)
这篇关于应用程序中使用的AsyncTask仍然占用UI线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!