android - 线程不在android服务中运行

标签 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 永远不会出现,运行中的断点也永远不会触发。 我错过了什么吗?

最佳答案

你没有启动线程

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

关于android - 线程不在android服务中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19298805/

相关文章:

java - 在服务层处理 Dao 异常

android - 有谁知道 name youtube 应用程序包在 android 中?

android - 找不到符号类 RTCClientVideoTracksCallback

java - 读取 PEM 证书链中的数据

java - setTextColor 的参数是一个 int 但可以接受 8 位十六进制值是怎么回事?

Android:闹钟每 30 分钟播放一次,从 12:30 开始

java - 干净的内部类实现 Runnable

c++ - 如何有条件地同时多线程和更新变量?

CreateThread 包装函数

grails - Grails调用服务错误