2017-04-25 51 views
0

我有一個無限的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

+0

「無限IntentService」 - 不這樣做。用你自己的線程池(或者,在這種情況下,也許是一個'ScheduledExecutorService'而不是線程池)創建你自己的'Service'。 – CommonsWare

+0

我嘗試過創建自己的'Service',但它阻止了我的UI –

+0

這是因爲您沒有設置自己的線程池,ScheduledExecutorService或其他將業務邏輯移動到後臺線程的東西。 「IntentService」專爲短暫的事務性工作而設計。 – CommonsWare

回答

0

感謝@CommonsWare我已成功地解決我的問題完全一樣bahavior 。
這是解決方案:

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; 
    } 
}