2017-02-06 1 views
0

목록이있어서이 목록의 모든 웹 사이트를 하나씩 반복하여 WebView에 루프로 표시해야합니다. 한 웹 사이트에서 다른 웹 사이트로 전환 할 때마다 동일한 크기의 다른 목록에도있는 개별 시간이 있습니다.Android - 서로 다른 시간대의 목록에서 웹보기의 URL을 표시합니다.

그러나 WebView에서 사이트를 표시하려고하면 웹 사이트를 건너 뛰고 시간이 정확하지 않을 수 있습니다. 누군가 내 코드를 개선하고 실수가있는 곳을 말해 줄 수 있습니까?

public class WebshowActivity extends AppCompatActivity { 
private List<String> htmlSites = new ArrayList<>(); 
private List<Integer> timeForSite = new ArrayList<>(); 
private int counter = 0; 
WebView webView; 
boolean firstCall = true; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_webshow); 
    //Fullscreen settings 
    View decorView = getWindow().getDecorView(); 
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN; 
    decorView.setSystemUiVisibility(uiOptions); 
    //implement list from utils 
    timeForSite.add(10);//this is the 0th index and it's needed for the first URL load which is emepty 
    for (int i = 0; i < Utilities.list.size(); i++) { 
     htmlSites.add(Utilities.list.get(i).getPath()); 
     timeForSite.add(Utilities.list.get(i).getDuration()); 
    } 
    //WebView 
    webView = (WebView) findViewById(R.id.webview); 
    WebSettings webSettings = webView.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    webView.loadUrl(""); 
    webView.setWebViewClient(new WebViewClient() { 
     public void onPageFinished(WebView view, String url) { 
      Toast.makeText(WebshowActivity.this, "Time for this Website: " + timeForSite.get(counter) + "ms", Toast.LENGTH_SHORT).show(); 
      final Handler handler = new Handler(); 
      handler.postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        webView.loadUrl(htmlSites.get(counter)); 
        counter++; 
        if (firstCall == true) { 
         timeForSite.remove(0); 
         firstCall = false; 
        } else { 
         counter++; 
        } 
        if (counter == htmlSites.size()) { 
         counter = 0; 
        } 
       } 
      }, timeForSite.get(counter)); 
     } 
    }); 
} 

}

목록은 다른 클래스에 있지만 나는 오류가 onPageFinished 방법 안에 생각합니다.

답변

0

업데이트 : Timer를 사용하여 더 정확하게 일정을 잡으려면. 그것은 영원히주기 것이다

public class MainActivity extends AppCompatActivity { 
    //mock 
    private static class SomeComplicatedObject { 
     private final String mPath; 
     private final Integer mDuration; 

     public SomeComplicatedObject(final String path, final int duration){ 
      mPath = path; 
      mDuration = duration; 
     } 

     String getPath() { return mPath; } 
     Integer getDureation() { return mDuration; } 
    } 

    private static final List<SomeComplicatedObject> complicatedList = new ArrayList<>(); 
    static { 
     complicatedList.add(new SomeComplicatedObject("http://google.com", 1000)); 
     complicatedList.add(new SomeComplicatedObject("http://facebook.com", 2000)); 
     complicatedList.add(new SomeComplicatedObject("http://google.com", 3000)); 
     complicatedList.add(new SomeComplicatedObject("http://twitter.com", 3000)); 
    } 

    private static final String TAG = "MainActivity"; 
    private final ArrayList<Pair<String, Integer>> mSitesToDisplay = new ArrayList<>(); 
    private final Timer mTimer = new Timer("LoadNext"); 
    private final Handler mHandler = new Handler(); 
    private WebView mWebView = null; 
    private int mIndex = -1; 

    private void fillSitesToDisplay(List<SomeComplicatedObject> list) { 
     mIndex = -1; 
     mSitesToDisplay.clear(); 
     for (SomeComplicatedObject item : list) { 
      mSitesToDisplay.add(new Pair<>(item.getPath(), item.getDureation())); 
     } 
    } 

    private Pair<String, Integer> getNext() { 
     mIndex = (mIndex+1) % mSitesToDisplay.size(); 
     return mSitesToDisplay.get(mIndex); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     fillSitesToDisplay(complicatedList); 
     View decorView = getWindow().getDecorView(); 
     int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN; 
     decorView.setSystemUiVisibility(uiOptions); 

     mWebView = (WebView)findViewById(R.id.webview); 

     //initial start delayed by 1 sec 
     mTimer.schedule(createTimerTask(), TimeUnit.SECONDS.toMillis(1)); 
    } 

    private Runnable mDisplayNextPage = new Runnable() { 
     @Override 
     public void run() { 
      final Pair<String, Integer> pathTimePair = getNext(); 
      Log.e(TAG, "DisplayNext::run : Start loading next page : " + pathTimePair.first); 
      mWebView.setWebViewClient(new WaitPageLoadedClient(mTimer, createTimerTask(), pathTimePair.second)); 
      mWebView.loadUrl(pathTimePair.first); 
     } 
    }; 

    private TimerTask createTimerTask() { 
     return new TimerTriggered(mHandler, mDisplayNextPage); 
    } 

    //simple utility class for notifying timer triggered event 
    //on UI thread. 
    private static class TimerTriggered extends TimerTask { 
     Runnable mOnTriggered; 
     Handler mHandler; 

     protected TimerTriggered(Handler handler, Runnable onTriggered) { 
      super(); 
      mHandler = handler; 
      mOnTriggered = onTriggered; 
     } 

     @Override 
     public void run() { 
      //Timer runs int it's own thread so we need to 
      //pass execution to MainThread for accessing WebView. 
      mHandler.post(mOnTriggered); 
     } 
    } 

    private static class WaitPageLoadedClient extends WebViewClient { 
     private final TimerTask mRunOnFinished; 
     private final Timer mTimer; 
     private final Integer mDelay; 

     public WaitPageLoadedClient(Timer timer, TimerTask toRunOnFinished, Integer delay) { 
      mRunOnFinished = toRunOnFinished; 
      mTimer = timer; 
      mDelay = delay; 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      Log.e(TAG, "onPageFinished: Schedule timer for " + mDelay); 
      mTimer.schedule(mRunOnFinished, mDelay); 
     } 
    } 
} 

: 여기

은 예입니다.

+0

대단히 감사합니다. 그것은 잘 작동하지만 시간에만 문제가 있습니다. 항상 일정하지는 않습니다. –

+0

처리기 대신 Timer 또는 CountDownTimer를 사용해야합니다. 핸들러는 UI 스레드에 의존하기 때문에 사전 검사가되지 않도록 – j2ko

+0

@htlstudent 대신 Timer를 사용하여 답변을 업데이트했습니다. 지금은 더 정확해야합니다. – j2ko

관련 문제