Android Page refresh in 20 seconds
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="23dp"
android:text="Button" />
</RelativeLayout>
---------------------------------------------------------------------------------
MainActivity-
package com.example.pagerefresh;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Handler handler;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(getApplicationContext(), "page refreshed", Toast.LENGTH_SHORT).show();
handler= new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "hello1", Toast.LENGTH_SHORT).show();
Intent i=new Intent(MainActivity.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
handler.removeCallbacksAndMessages(null);
handler.postDelayed(this, 4000);
}
}, 5000*4);//for 20sec
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
handler.removeCallbacksAndMessages(null);
super.onDestroy();
}
@Override
public void onStop() {
// TODO Auto-generated method stub
handler.removeCallbacksAndMessages(null);
super.onStop();
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="23dp"
android:text="Button" />
</RelativeLayout>
---------------------------------------------------------------------------------
MainActivity-
package com.example.pagerefresh;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Handler handler;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(getApplicationContext(), "page refreshed", Toast.LENGTH_SHORT).show();
handler= new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "hello1", Toast.LENGTH_SHORT).show();
Intent i=new Intent(MainActivity.this,MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
handler.removeCallbacksAndMessages(null);
handler.postDelayed(this, 4000);
}
}, 5000*4);//for 20sec
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
handler.removeCallbacksAndMessages(null);
super.onDestroy();
}
@Override
public void onStop() {
// TODO Auto-generated method stub
handler.removeCallbacksAndMessages(null);
super.onStop();
}
}
No comments:
Post a Comment