我刚刚开始开发一个Android应用程序,它上面有一个webview。我希望从特定时间开始每15分钟重新加载一次webview,如果我理解正确,我需要使用AlarmManager才能完成此任务。但我真的没有得到如何使用AlarmManager所以有人可以帮我这个吗?也许很少的例子或什么?
谢谢!
另外,仅供参考我查看了文件。
更新 所以我试图在这里实现AlarmManager ..并且不认为它正在工作...... 这是我的活动文件......
public class MainActivity extends Activity {
// to make our alarm manager
AlarmManager alarmManager;
WebView myWebView;
private PendingIntent pendingIntent;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
this.context = this;
//initialize alarm manager
//alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// create instance of a calendar
//Calendar calendar = Calendar.getInstance();
// create an intent to Alarm Receiver class
Intent alarmIntent = new Intent(this.context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("<some url goes in here>");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
reloadWebView();
}
private void reloadWebView(){
AlarmManager alarmManger = (AlarmManager)getSystemService(ALARM_SERVICE);
int interval = 1000*60*15;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 4);
calendar.set(Calendar.MINUTE,0);
alarmManger.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
}
}
这是AlarmReceiver类 公共类AlarmReceiver扩展了BroadcastReceiver {
WebView myWebView;
@Override
public void onReceive(Context context, Intent intent) {
myWebView.reload();
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}
谁能告诉我我做错了什么? 谢谢!
答案 0 :(得分:0)
您安排闹钟管理员在凌晨4点解雇您的意图。 使用RTC_WAKEUP使用当前系统时间+第一次火灾的间隔:
alarmManger.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pendingIntent);
同时检查您的警报接收器是否在Manifest中注册