我想使用一个服务进行后台下载我无法让线程在我的服务中启动。 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/