当没有断点时,无限IntentService停止工作

时间:2017-04-25 15:44:56

标签: android intentservice

我有一个无限的IntentService意味着在应用程序处于活动状态时始终启用扫描程序。只要有断点 - 它就可以正常工作。但是当我从循环中删除断点时 - 它会在一段时间后停止工作。然后当我提出一个断点时 - 它再次开始工作。有没有搞错?我该如何解决这个问题?

protected void onHandleIntent(@Nullable Intent intent)
{
    final Handler handler = new Handler();
    handler.post(new Runnable()
    {
        @Override
        public void run()
        {
            while(true)
            {
                if (!blocked)
                {
                    blocked = true;
                    String received = GlobalAccess.scan.scan(1000);

                    if (received != null && !received.isEmpty())
                    {
                        //TODO: some stuff here
                    }
                    blocked = false;
                }
                else
                {
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException ex)
                    {
                        ex.printStackTrace();
                    }
                }
            }
        }
    });
}

修改 我已使用

创建了CustomService
@Override
public void onCreate()
{
    super.onCreate();
    scheduledExecutorService = Executors.newScheduledThreadPool(1);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    final Handler handler = new Handler();
    Runnable runnable = new Runnable()
    {
        @Override
        public void run()
        {
            while(true)
            {
                if (!blocked)
                {
                    blocked = true;
                    String received = GlobalAccess.scan.scan(1000);

                    if (!Objects.equals(received, null) && !received.isEmpty())
                    {
                        //TODO: some stuff here
                    }
                    blocked = false;
                }
                else
                {
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException ex)
                    {
                        ex.printStackTrace();
                    }
                }
            }
        }
    };
    ScheduledFuture scheduledFuture = scheduledExecutorService.schedule(runnable, 0, TimeUnit.MILLISECONDS);

    return START_STICKY;
}

但它与IntentService

具有完全相同的行为

1 个答案:

答案 0 :(得分:0)

感谢@CommonsWare我设法解决了我的问题 这是解决方案:

public class ScanService extends Service
{
    ScheduledExecutorService scheduledExecutorService;

    @Override
    public void onCreate()
    {
        super.onCreate();
        scheduledExecutorService = Executors.newScheduledThreadPool(1);
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                String received = GlobalAccess.scan.scan(1000);

                if (!Objects.equals(received, null) && !received.isEmpty())
                {
                    //TODO: some stuff here
                }
            }
        };
        scheduledExecutorService.scheduleWithFixedDelay(runnable, 0, 50, TimeUnit.MILLISECONDS);

        return START_STICKY;
    }
}