Issue
I'm developing a basic app for a personal project, which includes a digital clock, an analog clock, a date display and a weather widget, I have made the weather widget using WebView object, which loads up MSN Weather. The only thing now left is to get the WebView to refresh itself after every 15 mins.
I have read various StackOverflow posts but not able to implement any of those (I'm a newbie to app development, everything I have written in the code is by taking help from Android Development Docs or from here)
My WebView XML code :
<WebView
android:id="@+id/webview"
android:layout_width="0dp"
android:layout_height="352dp"
android:layout_marginStart="35dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="35dp"
android:layout_marginBottom="25dp"
android:overScrollMode="never"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.65"
app:layout_constraintStart_toEndOf="@+id/digitalclock"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.666"></WebView>
My MainActivity.java:
public class MainActivity extends AppCompatActivity { private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.msn.com/en-in/weather/today/Indira-Puram,Uttar-Pradesh,India/we-city?form=PRWLAS&iso=IN&el=BrSTQuEAtMKg0ieFIZL7dsSW4wabraPd56ExjVOnG498zXJC6snI8phK%2Fm3kngtYUxMgtIHuTpASUdkcj74e18ed11%2FJGPgvtNUExJNmB6MbbUPuzf2%2B2TVwoTiWCxnz");
webView.reload();
Calendar calendar = Calendar.getInstance();
String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
TextView textviewdate = findViewById(R.id.text_date);
textviewdate.setText(currentDate);
}
}
Is there any simple way of doing the Auto-Refresh thing, without any user interaction?
Thanks in advance
Solution
create one method outside of onCreate refresh()
public void refresh() {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
webView.loadUrl("https://www.msn.com/en-in/weather/today/Indira-Puram,Uttar-Pradesh,India/we-city?form=PRWLAS&iso=IN&el=BrSTQuEAtMKg0ieFIZL7dsSW4wabraPd56ExjVOnG498zXJC6snI8phK%2Fm3kngtYUxMgtIHuTpASUdkcj74e18ed11%2FJGPgvtNUExJNmB6MbbUPuzf2%2B2TVwoTiWCxnz");
//webView.reload();
Calendar calendar = Calendar.getInstance();
String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
textviewdate.setText(currentDate);
}
}, 50000);//increase if want more time
call refresh()
in oncreate method
every 5 seconds it will refreshing
Answered By - Ajithkumar Muthukumaran
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.