1

我想使用一项服务进行后台下载,我无法在我的服务中启动线程。从不调用 run 方法(我尝试过本地和单独的进程服务)

    public class DownloadService extends Service
    {
        private int count = 0;

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return Service.START_NOT_STICKY;
        }


        @Override
        public void onCreate()
        {
            super.onCreate();
            //mDT.start(getApplicationContext(), new Handler());
            new Thread(new DownloadRunnable());
        }


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



        private class DownloadRunnable implements Runnable
        {
            @Override
            public void run()
            {
                ++count;
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Hey from Service", Toast.LENGTH_SHORT).show();
                    }
                });
                ++count;
            }
        }
    }

//inside application class
Intent i= new Intent(this, DownloadService.class);
startService(i);

toast 永远不会出现,并且 run 内部的断点永远不会被触发。我错过了什么吗?

4

1 回答 1

4

你不启动线程

new Thread(new DownloadRunnable()).start()
于 2013-10-10T14:38:37.700 回答