PrintStatusTask解析gmail收件箱中的内容,查找要报告的各种感兴趣的项目.但是根据调试器为每个PrintStatusTask().execute()创建一个新的AsyncTask.这些任务不应该在退出时死亡吗?他们必须手动杀死吗?
public class Controller extends Activity {
...
private boolean applyMenuChoice(MenuItem item) {
switch (item.getItemId()) {
case R.id.menuStatus:
new PrintStatusTask().execute();
...
class PrintStatusTask extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... unused) {
...
return null;
}
@Override
protected void onPostExecute(Void unused) {
this.cancel(true);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个在一个活动上调用的AsyncTask(在一个单独的文件中).当我实例化AsyncTask时,我将活动作为参数发送.如何从AsyncTask的onPostExecute方法访问acitivity的实例变量?
谢谢!
我有一个异步任务,用于连接到网站(Twitter).在某些情况下(例如,当Twitter发布更新失败时)我想等待10秒才再次尝试.因为我已经处于异步任务中,所以我不想为计时器启动另一个线程.
任何建议都会很棒:).代码如下......
class SendTwitAsyncTask extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String tokenTwit = params[0];
String tokenSecretTwit = params[1];
String strMessageBody = params[2];
AccessToken aToken = new AccessToken(tokenTwit, tokenSecretTwit);
// initialize Twitter4J
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(aToken);
aToken=null;
// create a tweet
Date d = new Date(System.currentTimeMillis());
String tweet = strMessageBody;
try {
status = twitter.updateStatus(tweet);
// feedback for the user..
showNotification("Twitter", TWIT_SUCCESS);
} catch (TwitterException te) {
showNotification("Twitter ", TWIT_FAIL);
te.printStackTrace();
//TO …Run Code Online (Sandbox Code Playgroud) 是否有可能onProgressUpdate错过了一些调用publishProgress?我的意思是使用publishProgress调用可能在doInBackground和onProgressUpdate回调之间有一个传输未命中.因为我看到了.
class DoSomething extends AsyncTask Void, String, Void {
String[] S = new String[] {"a", "b", "c", "d"};
void doInBackground(Void... ps) {
for(String s : S) {
publishProgress(s);
}
}
void onProgressUpdate(String... vs) {
Log.d("", vs[0]);
}
Run Code Online (Sandbox Code Playgroud)
我遇到的结果是什么
abbd
目前,我正在AsycTask中执行一项繁重的任务,上传图像.当我通过AsycTask执行此操作时,应用程序将关闭,并且logcat中不会显示任何错误消息.
private class SendIncidentTask extends AsyncTask<Double, Void, Void>{
@Override
protected Void doInBackground(Double... params) {
lat1 = params[0];
log1 = params[1];
SendingIncident sincident = new SendingIncident();
sincident.sendInciWithoutAttachment(lat1, log1);
sincident.submitWithAttachment(lat1, log1);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为当UI线程和AsycTask并行执行时会出现问题.如何让UI线程等到AsycTask结束?
提前致谢.
我需要在AsyncTask的doInBackground中更新数百条记录.我想在每10条记录中更新我的列表视图.但是我们不能在doInBackground中调用notifyDataSetChanged.所以无论如何都要在doInBackground中调用notifyDataSetChanged?
我正在考虑注册BroadcastReceiver.我们在doInBackground中完成每10条记录时发送一个Intent,并在BroadcastReceiver中调用notifyDataSetChanged?有什么建议吗?
如果他们只对它执行查询?就像是:
private SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
db = getWritableDatabse();
}
private Task1 extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
db.query(doSomething);
...
}
}
private Task2 extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
db.query(doSomething);
...
}
}
Run Code Online (Sandbox Code Playgroud) 我试图在onPreExecute()中启动ProgressDialog时出现AsyncTask,做一些事情doInBackground(),并关闭ProgressDialogin onPostExecute().
在任何地方,我认为这应该是简单而直接的,但我必须做错事.
每当我点击Button我的应用程序进行更新并AsyncTask运行应用程序冻结,等待一点,然后ProgressDialog立即出现并消失,工作doInBackground()完成.我不明白为什么onPreExecute()会被追赶doInBackground(),但这似乎正是这里发生的事情.这是代码:
public class UpdateStats extends AsyncTask<CatchAPI, Void, String[]> {
private ProgressDialog pdia;
private Context context;
private APIParser parser = new APIParser();
public UpdateStats(Context contexts) {
context = contexts;
}
@Override
protected void onPreExecute() {
pdia = new ProgressDialog(context);
pdia.setMessage("Loading...");
pdia.show();
super.onPreExecute();
}
@Override
protected String[] doInBackground(CatchAPI... api) {
String apiOutput = api[0].returnAPI();
return new String[] { …Run Code Online (Sandbox Code Playgroud) 我已经使用了AsyncTask,但我不明白为什么在我的设备(OS 4.0)上测试时仍然会出现错误.我的apk版本在2.3.3中.我想我的代码错了,但我不知道我的错误在哪里.有人请帮帮我,非常感谢你
login.java
package com.karismaelearning;
public class Login extends Activity {
public Koneksi linkurl;
String SERVER_URL;
private Button login, register, setting;
private EditText username, password;
public ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
setting = (Button)findViewById(R.id.bsetting);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.reg);
username = (EditText) findViewById(R.id.uname);
password = (EditText) findViewById(R.id.pass);
setting.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intentSet = new Intent(Login.this, UrlSetting.class);
startActivity(intentSet);
}
});
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个本机登录屏幕,其中用户名和密码框在处理请求时滑出屏幕(如果登录失败则向上滑动).
为了实现这一点,我已经定义了我的animation(DropDownAnimation)并将其分配给我的LinearLayout(页脚).当用户单击"登录"按钮时,我启动动画,然后调用一个函数(tryLogin())启动AsyncTask.该AsyncTask手柄创建和发送的登录请求,并获得了所有的工作JSONObject响应.
但是,我的问题是slideDown动画直到AsyncTask完成后才开始.成功登录时看起来并不是那么糟糕,但是登录失败意味着LinearLayout从不向下滑动 - 它会跳到屏幕底部,开始slideUp动画回到原来的位置.
对于这个问题,这似乎是一个类似的问题,但我没有使用bindService(),我的所有非UI代码似乎(对我来说)AsyncTask已经包含在内.LogCat告诉我:
06-24 04:37:35.141: I/Choreographer(5347): Skipped 137 frames! The application may be doing too much work on its main thread.
我假设这些是页脚向下滑动的框架 - 但我无法弄清楚我在主线程上执行的是什么.这是我的LoginPage和LoginTask的代码.
LoginPage.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
login = (Button) findViewById(R.id.login);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
footer = (LinearLayout) findViewById(R.id.footer);
// We …Run Code Online (Sandbox Code Playgroud)