线程不在android服务中运行

时间:2013-10-10 14:37:35

标签: android multithreading service

我想使用服务进行后台下载我无法在我的服务中启动线程。永远不会调用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永远不会出现,并且运行中的断点永远不会被触发。 我想念一下吗?

1 个答案:

答案 0 :(得分:3)

你不开始线程

new Thread(new DownloadRunnable()).start()