2013-03-06 112 views
0

我有一個非常簡單的webview佈局。Android在特定時間或日期定期更改webview內容

我想每1小時更換一次webview內容。

儘量選用

Uri uri = Uri.parse("http://www.yahoo.com"); 
    Intent in = new Intent(Intent.ACTION_VIEW, uri); 
    context.startActivity(in); 
在廣播接收機

,但未能.....

任何sugguestions?謝謝!!!!

主要活動代碼:

public class MainActivity extends Activity implements OnClickListener { 

private int page; 

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

    RelativeLayout layout = (RelativeLayout) 
    findViewById(R.id.RelativeLayout1); 
    layout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 

    showeb("http://www.google.com"); 

    Intent intent =new Intent(MainActivity.this, Alarm.class); 
    intent.setAction("repeating"); 
    PendingIntent sender=PendingIntent 
     .getBroadcast(MainActivity.this, 0, intent, 0); 

    long firstime=SystemClock.elapsedRealtime(); 

    AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE); 
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP 
      , firstime, 3600*1000, sender); 


} 

private void showeb(String string) { 
    WebView engine = (WebView) (findViewById(R.id.webView1)); 
    engine.setWebViewClient(new WebViewClient()); 
    engine.loadUrl(string); 

} 

和接收機類

public class Alarm extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

     Toast.makeText(context, "short alarm", Toast.LENGTH_LONG).show(); 


     Uri uri = Uri.parse("http://www.yahoo.com"); 
     Intent in = new Intent(Intent.ACTION_VIEW, uri); 
     context.startActivity(in); 



} 

回答

0

如果您要更新的WebView這已經是在前臺(當前顯示的),那麼你可以定期請撥打webview.loadUrl只需使用java.util.TimerTask

private Timer webViewUpdater; 

    @Override 
    public void onResume() { 
     super.onResume(); 
     webViewUpdater = new Timer(); 
     webViewUpdater.schedule(new TimerTask() { 
      @Override 
      public void run() { 
       runOnUiThread(new Runnable() { 
        public void run() { 
         showeb("http://www.yahoo.com") 
        } 
       }); 
      } 
     }, firsttime, 3600*1000); 
    } 

    @Override 
    public void onPause() { 
     webViewUpdater.cancel(); 
     super.onPause(); 
    } 
+0

對不起,bu我應該在哪裏放這些代碼?我可以設置webview在特定的日期和時間更改嗎? (例如週一下午5點)非常感謝!!!!! – 2013-03-07 02:05:39

+0

你只需要重寫Activity的onResume()方法。可以在代碼中定期檢查,然後與您的Mon,5PM比較並在必要時重新加載。 – MartinC 2014-02-28 09:08:00