我想实现这个功能:
有后台服务:
1)当网络脱机时,后台服务继续检测,如果恢复网络,刷新webView,恢复显示。2)当网络正常时,后台服务将继续检测,如果给后台服务器一个新的URL,则刷新当前页面。
答案 0 :(得分:0)
您可以使用广播接收器以及AlarmManager和PendingIntent。将警报重复设置为所需的持续时间,并在该持续时间内触发待处理的意图。
public void scheduleAlarmForURLAutoUpdate() {
Long time = new GregorianCalendar().getTimeInMillis()+1000 * 60 * 60 * 24;// current time + 24 Hrs
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent intentAlarm = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 1000 * 60 * 60 * 24, intentAlarm);// 24 Hrs
//Toast.makeText(this, "Alarm Scheduled for 24 Hrs", Toast.LENGTH_LONG).show();
}
AlarmReceiver Class
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
updateURL(context);//Method to update the URL from the server and then refresh your WebView activity
}
答案 1 :(得分:0)
公共类AlarmReceiver扩展了BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, AutoUpdateService.class);
context.startService(i);
}
}