【问题标题】:Android Async tasks don't work when used in a service?Android Async 任务在服务中使用时不起作用?
【发布时间】:2012-01-22 14:35:45
【问题描述】:

我正在设置一个进行网络调用的后台任务;但是,由于我无法让我设置的 AsyncTask 工作,我的开发已经停滞,我所做的是我在代码中的各个点放置了 toast 以查看问题发生的地方。我意识到,直到 AsyncTask 的预执行方法为止,我都会得到一个祝酒词;但是,在doInBackgroundpostexecute 中,我什么也得不到。

这是为什么?如果这不能完成,有什么解决方法?

package com.testapp2.second.activities;

import java.util.Date;

import com.testapp2.second.OTweetApplication;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.widget.Toast;

public class StatsCheckerService extends Service {
    private OTweetApplication app;
    private Twitter twitter;
    User user;

    @Override
    public IBinder onBind(Intent arg0) {
       return null;
    }

    @Override
    public void onCreate() {
       super.onCreate();
       app = (OTweetApplication) getApplication();
       twitter = app.getTwitter();

       Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (app.isAuthorized()) {

            Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG)
                .show();
            new toastTwitterInBg();
        }
        return START_STICKY;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    }

    private class toastTwitterInBg extends AsyncTask<Void, Void, User> {

        @Override
        protected User doInBackground(Void... arg0) {
            try {
                user = twitter.verifyCredentials();
                Toast.makeText(StatsCheckerService.this, "async tasks do in background", Toast.LENGTH_LONG).show();
            } catch (TwitterException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return user;
        }

        @Override
        protected void onPostExecute(User result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            Toast.makeText(StatsCheckerService.this, "async tasks on post execute finished", Toast.LENGTH_LONG).show();
        }

    }
} 

【问题讨论】:

    标签: java android android-asynctask android-service


    【解决方案1】:

    正如其他人已经指出的那样,toast 在doInBackground 中不起作用,因为它在单独的线程上运行。另外,你真的在​​执行你的AsyncTask吗?

    这不应该是:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
      // ....
      new toastTwitterInBg().execute();
      // ....
    }
    

    注意.execute()

    【讨论】:

      【解决方案2】:

      Toast 只能从 UI 线程抛出。 doInBackground 在非 UI 线程上运行,因此抛出 Toast 不会有任何影响。

      onPostExecute 中没有得到任何东西很奇怪!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-03
        • 2012-01-25
        • 2016-11-08
        相关资源
        最近更新 更多