0

我有一个非常简单的 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);



}
4

1 回答 1

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();
    }
于 2013-03-06T12:04:36.150 回答