Android会定期在特定时间或日期更改网页内容

时间:2013-03-06 10:53:41

标签: android android-webview alarmmanager

我有一个非常简单的webview布局。

我想每1小时更改一次webview内容。

试图使用

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

在广播接收器中,但失败了.....

有任何建议吗?感谢!!!!

主要活动代码:

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



}

1 个答案:

答案 0 :(得分: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();
    }