If I have used threads instead of an async task, then how do I refresh data periodically while navigating through activities? My data is read and parsed in the main activity.
public class MainActivity extends Activity
{
//data_bedpres_future stores future data from current date
public static ArrayList<ArrayList<String>> data_bedpres_future = new ArrayList<ArrayList<String>>();
//data_bedpres_past stores past data from current date
public static ArrayList<ArrayList<String>> data_bedpres_past = new ArrayList<ArrayList<String>>();
//data_nyheter_future stores future data from current date
public static ArrayList<ArrayList<String>> data_nyheter_future = new ArrayList<ArrayList<String>>();
//data_nyheter_past stores past data from current date
public static ArrayList<ArrayList<String>> data_nyheter_past = new ArrayList<ArrayList<String>>();
private Handler mHandler = new Handler();
private ArrayList<Date> mydate = new ArrayList<Date>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ProgressBar progress_waiting1=(ProgressBar)findViewById(R.id.progress1);
progress_waiting1.setVisibility(ProgressBar.VISIBLE);
final ProgressBar progress_waiting2=(ProgressBar)findViewById(R.id.progress2);
progress_waiting2.setVisibility(ProgressBar.VISIBLE);
Typeface tf_helvetica = Typeface.createFromAsset(getAssets(),"fonts/HelveticaNw.ttf");
Typeface tf_helvetica_bold = Typeface.createFromAsset(getAssets(),"fonts/HelveticaNwBd.ttf");
Config.TF_HELVETICA=tf_helvetica;
Config.TF_HELVETICA_BOLD=tf_helvetica_bold;
final ImageButton bttn_bedpress=(ImageButton)findViewById(R.id.bttn_bedpress);
ImageButton bttn_news=(ImageButton)findViewById(R.id.bttn_news);
ImageButton bttn_logo=(ImageButton)findViewById(R.id.bttn_logo);
ImageButton bttn_faq=(ImageButton)findViewById(R.id.bttn_faq);
ImageButton bttn_cv=(ImageButton)findViewById(R.id.bttn_cv);
ImageButton bttn_omnu=(ImageButton)findViewById(R.id.bttn_omnu);
ImageButton bttn_recent=(ImageButton)findViewById(R.id.button1);
final RelativeLayout img_gray_bg=(RelativeLayout)findViewById(R.id.img_gray_bg);
img_gray_bg.setVisibility(ImageView.VISIBLE);
final TextView txt_wait=(TextView)findViewById(R.id.txt_wait);
txt_wait.setVisibility(TextView.VISIBLE);
final TextView topheading=(TextView)findViewById(R.id.txt_topheading);
topheading.setTypeface(Config.TF_HELVETICA_BOLD);
final TextView headline=(TextView)findViewById(R.id.txt_headline);
headline.setTypeface(Config.TF_HELVETICA_BOLD);
final TextView date=(TextView)findViewById(R.id.txt_date);
date.setTypeface(Config.TF_HELVETICA_BOLD);
final TextView txt_place=(TextView)findViewById(R.id.txt_place);
txt_place.setTypeface(Config.TF_HELVETICA_BOLD);
final ImageView img_company=(ImageView)findViewById(R.id.img_company);
final TextView txt_news=(TextView)findViewById(R.id.txt_news);
txt_news.setTypeface(Config.TF_HELVETICA_BOLD);
final TextView txt_career=(TextView)findViewById(R.id.txt_career);
txt_career.setTypeface(Config.TF_HELVETICA_BOLD);
final TextView txt_bedpress=(TextView)findViewById(R.id.txt_bedpress);
txt_bedpress.setTypeface(Config.TF_HELVETICA_BOLD);
final TextView txt_cv=(TextView)findViewById(R.id.txt_cv);
txt_cv.setTypeface(Config.TF_HELVETICA_BOLD);
final TextView txt_faq=(TextView)findViewById(R.id.txt_faq);
txt_faq.setTypeface(Config.TF_HELVETICA_BOLD);
final TextView txt_logo=(TextView)findViewById(R.id.txt_logo);
txt_logo.setTypeface(Config.TF_HELVETICA_BOLD);
if(!HaveNetworkConnection())
{
Log.v("no network","no network");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Nettverk ikke tilgjengelig")
.setCance开发者_JAVA百科lable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
final Thread parseThread=new Thread(new Runnable() {
public void run()
{
if(HaveNetworkConnection())
{
parseDataBedPres(Config.URL_BedPres);
parseDataNyheter(Config.URL_Nyheter);
Collections.sort(mydate);
}
else
{
Log.v("net not connected","net not connected");
parseDataBedPresLocal();
parseDataNyheterLocal();
/*
Looper.prepare();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Nettverk ikke tilgjengelig")
.setCancelable(false)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();*/
}
}
});
parseThread.start();
Thread displayThread = new Thread(new Runnable() {
public void run() {
try
{
parseThread.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
mHandler.post(new Runnable() {
public void run()
{
// TODO Auto-generated method stub
progress_waiting1.setVisibility(ProgressBar.GONE);
progress_waiting2.setVisibility(ProgressBar.GONE);
txt_wait.setVisibility(TextView.GONE);
img_gray_bg.setVisibility(ImageView.GONE);
String month[]=getResources().getStringArray(R.array.month_array);
if(data_bedpres_future.size()!=0)
{
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date dt = null;
Calendar cal = Calendar.getInstance();
try
{
dt = df.parse(data_bedpres_future.get(1).get(0));
cal.setTime(dt);
}
catch (ParseException e1)
{
e1.printStackTrace();
}
DateFormat df2=new SimpleDateFormat("hh:mm:ss");
Date dt2 = null;
Calendar cal2 = Calendar.getInstance();
try
{
dt2 = df2.parse(data_bedpres_future.get(2).get(0));
cal2.setTime(dt2);
}
catch (ParseException e1)
{
e1.printStackTrace();
}
date.setText("Tid: "+cal.get(Calendar.DATE)+" "+month[cal.get(Calendar.MONTH)]+" kl "+cal2.get(Calendar.HOUR_OF_DAY)+":"+cal2.get(Calendar.MINUTE));
//date.setText("Tid : "+data_bedpres_future.get(1).get(0));
headline.setText("Neste Bed.pres: "+data_bedpres_future.get(0).get(0));
txt_place.setText("Sted: "+data_bedpres_future.get(3).get(0));
try
{
Drawable img = drawable_from_url(data_bedpres_future.get(4).get(0),"icon");
if(img!=null)
{
int height,width,w;
height = img.getMinimumHeight() > 52 ? 52 :img.getMinimumHeight();
int ratio = (((img.getMinimumHeight() - height)*100)/img.getMinimumHeight());
int wi = (img.getMinimumWidth() * ratio) /100;
width = img.getMinimumWidth() -wi;
w=width;
int width1 = width > 211 ? 211 :width;
if (width>0)
{
ratio = (((width - width1)*100)/width);
int hi = (height * ratio) /100;
height = height -hi;
w=width1;
}
ViewGroup.LayoutParams params = img_company.getLayoutParams();
params.height = height;
params.width = w;
img_company.setLayoutParams(params);
img_company.setBackgroundDrawable(drawable_from_url(data_bedpres_future.get(4).get(0),"icon"));
}
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
else if(data_nyheter_future.size()!=0)
{
date.setText(data_nyheter_future.get(2).get(0));
headline.setText("Siste nyhet: "+data_nyheter_future.get(3).get(0));
try
{
Drawable img = drawable_from_url(data_nyheter_future.get(0).get(0),"icon");
if(img!=null)
{
int height,width,w;
height = img.getMinimumHeight() > 52 ? 52 :img.getMinimumHeight();
int ratio = (((img.getMinimumHeight() - height)*100)/img.getMinimumHeight());
int wi = (img.getMinimumWidth() * ratio) /100;
width = img.getMinimumWidth() -wi;
w=width;
int width1 = width > 211 ? 211 :width;
if (width>0)
{
ratio = (((width - width1)*100)/width);
int hi = (height * ratio) /100;
height = height -hi;
w=width1;
}
ViewGroup.LayoutParams params = img_company.getLayoutParams();
params.height = height;
params.width = w;
img_company.setLayoutParams(params);
img_company.setBackgroundDrawable(img);
}
}catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
else if(data_nyheter_past.size()!=0)
{
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date dt = null;
Calendar cal = Calendar.getInstance();
try
{
dt = df.parse(data_nyheter_past.get(2).get(0));
cal.setTime(dt);
}
catch (ParseException e1)
{
e1.printStackTrace();
}
date.setText(""+cal.get(Calendar.YEAR)+"-"+month[cal.get(Calendar.MONTH)]+"-"+cal.get(Calendar.DATE));
headline.setText("Siste nyhet: "+data_nyheter_past.get(3).get(0));
try
{
Drawable img = drawable_from_url(data_nyheter_past.get(0).get(0),"icon");
if(img!=null)
{
int height,width,w;
height = img.getMinimumHeight() > 52 ? 52 :img.getMinimumHeight();
int ratio = (((img.getMinimumHeight() - height)*100)/img.getMinimumHeight());
int wi = (img.getMinimumWidth() * ratio) /100;
width = img.getMinimumWidth() -wi;
w=width;
int width1 = width > 211 ? 211 :width;
if (width>0)
{
ratio = (((width - width1)*100)/width);
int hi = (height * ratio) /100;
height = height -hi;
w=width1;
}
ViewGroup.LayoutParams params = img_company.getLayoutParams();
params.height = height;
params.width = w;
img_company.setLayoutParams(params);
img_company.setBackgroundDrawable(img);
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
});
}
});
displayThread.start();
bttn_bedpress.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//bttn_bedpress.setBackgroundDrawable(getResources().getDrawable(R.drawable.bedpres_pressed));
if(data_bedpres_future.size()!=0)
{
Log.v("size of future data","size="+data_bedpres_future.size());
startActivity(new Intent("com.vyam.BusinessPresentations"));
}
else
{
startActivity(new Intent("com.vyam.BusinessPresentationsPast"));
}
}
});
bttn_news.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if(data_nyheter_future.size()!=0)
{
Log.v("size of future data","size="+data_nyheter_future.size());
startActivity(new Intent("com.vyam.FutureNews"));
}
else
{
startActivity(new Intent("com.vyam.PastNews"));
}
}
});
bttn_logo.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent("com.vyam.CareerFair"));
}
});
bttn_faq.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent("com.vyam.FAQ"));
}
});
bttn_cv.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent("com.vyam.CV"));
}
});
bttn_omnu.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent("com.vyam.OmNU"));
}
});
bttn_recent.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Intent intent=new Intent("com.vyam.NewsDetail");
intent.putExtra("type","past");//it sends information that data must be shown from data_bedpres_past
intent.putExtra("position",0);
startActivity(intent);
}
});
}
/**
* the parseDataBedPres(String url)method does JSON parsing of the url Config.URL_BedPres
* @return void
*/
public void parseDataBedPres(String url)
{
ArrayList<String> company_names_future = new ArrayList<String>();
ArrayList<String> company_names_past = new ArrayList<String>();
ArrayList<String> date_future = new ArrayList<String>();
ArrayList<String> date_past = new ArrayList<String>();
ArrayList<String> place_future = new ArrayList<String>();
ArrayList<String> place_past = new ArrayList<String>();
ArrayList<String> meeting_time_future = new ArrayList<String>();
ArrayList<String> meeting_time_past = new ArrayList<String>();
ArrayList<String> company_logo_future = new ArrayList<String>();
ArrayList<String> company_logo_past = new ArrayList<String>();
ArrayList<String> description_future = new ArrayList<String>();
ArrayList<String> description_past = new ArrayList<String>();
ArrayList<String> url_future = new ArrayList<String>();
ArrayList<String> url_past = new ArrayList<String>();
ArrayList<String> fb_id_future = new ArrayList<String>();
ArrayList<String> fb_id_past = new ArrayList<String>();
FileOutputStream fos=null;
try {
URL business_url = new URL(url);
URLConnection tc = business_url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
StringBuffer fileData=new StringBuffer();
String line;
while ((line = in.readLine()) != null)
{
JSONArray ja = new JSONArray(line);
Log.v("line"," "+line);
fileData.append(line);
for (int i = 0; i < ja.length(); i++)
{
JSONObject jo = (JSONObject) ja.get(i);
try
{
Date today = new Date();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm");
//Date date = df.parse(jo.getString("meeting_date"));
StringBuilder s1 = new StringBuilder(jo.getString("meeting_date"));
s1.append(" ");
s1.append(jo.getString("meeting_time"));
Date date = df.parse(s1.toString());
if(today.after(date))
{
if(jo.getString("company_name")!=null)
company_names_past.add(jo.getString("company_name"));
else
company_names_past.add(" ");
if(jo.getString("meeting_date")!=null)
date_past.add(jo.getString("meeting_date"));
else
date_past.add(jo.getString(" "));
if(jo.getString("meeting_address")!=null)
place_past.add(jo.getString("meeting_address"));
else
place_past.add(" ");
if(jo.getString("meeting_time")!=null)
meeting_time_past.add(jo.getString("meeting_time"));
else
meeting_time_past.add(jo.getString(" "));
if(jo.getString("company_logo")!=null)
company_logo_past.add(jo.getString("company_logo"));
else
company_logo_past.add(jo.getString(" "));
if(jo.getString("description")!=null)
description_past.add(jo.getString("description"));
else
description_past.add(jo.getString(" "));
if(jo.getString("company_url")!=null)
url_past.add(jo.getString("company_url"));
else
url_past.add(jo.getString(" "));
if(jo.getString("fb_event_id")!=null)
fb_id_past.add(jo.getString("fb_event_id"));
else
fb_id_past.add(jo.getString(" "));
}
else
{
if(jo.getString("company_name")!=null)
company_names_future.add(jo.getString("company_name"));
else
company_names_future.add(" ");
if(jo.getString("meeting_date")!=null)
date_future.add(jo.getString("meeting_date"));
else
date_future.add(jo.getString(" "));
if(jo.getString("meeting_address")!=null)
place_future.add(jo.getString("meeting_address"));
else
place_past.add(" ");
if(jo.getString("meeting_time")!=null)
meeting_time_future.add(jo.getString("meeting_time"));
else
meeting_time_future.add(jo.getString(" "));
if(jo.getString("company_logo")!=null)
company_logo_future.add(jo.getString("company_logo"));
else
company_logo_future.add(jo.getString(" "));
if(jo.getString("description")!=null)
description_future.add(jo.getString("description"));
else
description_future.add(jo.getString(" "));
if(jo.getString("company_url")!=null)
url_future.add(jo.getString("company_url"));
else
url_future.add(jo.getString(" "));
if(jo.getString("fb_event_id")!=null)
fb_id_future.add(jo.getString("fb_event_id"));
else
fb_id_future.add(jo.getString(" "));
}
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
writeInternalStoragePrivate("BedPres",fileData.toString().getBytes());
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (JSONException e)
{
e.printStackTrace();
}
if(company_names_future.size()!=0)
{
data_bedpres_future.add(company_names_future);
data_bedpres_future.add(date_future);
data_bedpres_future.add(meeting_time_future);
data_bedpres_future.add(place_future);
data_bedpres_future.add(company_logo_future);
data_bedpres_future.add(description_future);
data_bedpres_future.add(url_future);
data_bedpres_future.add(fb_id_future);
}
if(company_names_past.size()!=0)
{
data_bedpres_past.add(company_names_past);
data_bedpres_past.add(date_past);
data_bedpres_past.add(meeting_time_past);
data_bedpres_past.add(place_past);
data_bedpres_past.add(company_logo_past);
data_bedpres_past.add(description_past);
data_bedpres_past.add(url_past);
data_bedpres_past.add(fb_id_past);
}
Log.v("hello files----"," "+getFilesDir().getPath());
}
/**
* This parses the url for Nyheter
* @param url
* @return
*/
public void parseDataNyheter(String url)
{
ArrayList<ArrayList<String>> data=new ArrayList<ArrayList<String>>();
ArrayList<String> image1_future = new ArrayList<String>();
ArrayList<String> image1_past = new ArrayList<String>();
ArrayList<String> image2_future = new ArrayList<String>();
ArrayList<String> image2_past = new ArrayList<String>();
ArrayList<String> date_future = new ArrayList<String>();
ArrayList<String> date_past = new ArrayList<String>();
ArrayList<String> headline_future = new ArrayList<String>();
ArrayList<String> headline_past = new ArrayList<String>();
ArrayList<String> ingress_text_future = new ArrayList<String>();
ArrayList<String> ingress_text_past = new ArrayList<String>();
ArrayList<String> body_text_future = new ArrayList<String>();
ArrayList<String> body_text_past = new ArrayList<String>();
//ArrayList<Date> mydate = new ArrayList<Date>();
FileOutputStream fos=null;
try {
URL business_url = new URL(url);
URLConnection tc = business_url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream()));
StringBuffer fileData=new StringBuffer();
String line;
while ((line = in.readLine()) != null)
{
fileData.append(line);
JSONArray ja = new JSONArray(line);
Log.v("line"," "+line);
for (int i = 0; i < ja.length(); i++)
{
JSONObject jo = (JSONObject) ja.get(i);
try
{
Date today = new Date();
Log.v("todays date","date--"+today);
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date date1 = df.parse(jo.getString("date"));
if(today.after(date1))
{
if(jo.getString("image_1")!=null)
image1_past.add(jo.getString("image_1"));
else
image1_past.add(" ");
if(jo.getString("image_2")!=null)
image2_past.add(jo.getString("image_2"));
else
image2_past.add(" ");
if(jo.getString("date")!=null)
date_past.add(jo.getString("date"));
else
date_past.add(" ");
if(jo.getString("headline")!=null)
headline_past.add(jo.getString("headline"));
else
ingress_text_past.add(" ");
if(jo.getString("ingress_text")!=null)
ingress_text_past.add(jo.getString("ingress_text"));
else
headline_past.add(" ");
if(jo.getString("body_text")!=null)
body_text_past.add(jo.getString("body_text"));
else
body_text_past.add(" ");
}
else
{
if(jo.getString("image_1")!=null)
image1_future.add(jo.getString("image_1"));
else
image1_future.add(" ");
if(jo.getString("image_2")!=null)
image2_future.add(jo.getString("image_2"));
else
image2_future.add(" ");
if(jo.getString("date")!=null)
date_future.add(jo.getString("date"));
else
date_future.add(" ");
if(jo.getString("headline")!=null)
headline_future.add(jo.getString("headline"));
else
headline_future.add(" ");
if(jo.getString("ingress_text")!=null)
ingress_text_future.add(jo.getString("ingress_text"));
else
ingress_text_future.add(" ");
if(jo.getString("body_text")!=null)
body_text_future.add(jo.getString("body_text"));
else
body_text_future.add(" ");
}
}
catch (ParseException e)
{
e.printStackTrace();
}
}
}
writeInternalStoragePrivate("Nyheter.txt",fileData.toString().getBytes());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(headline_future.size()!=0)
{
data_nyheter_future.add(image1_future);
data_nyheter_future.add(image2_future);
data_nyheter_future.add(date_future);
data_nyheter_future.add(headline_future);
data_nyheter_future.add(ingress_text_future);
data_nyheter_future.add(body_text_future);
}
if(headline_past.size()!=0)
{
data_nyheter_past.add(image1_past);
data_nyheter_past.add(image2_past);
data_nyheter_past.add(date_past);
data_nyheter_past.add(headline_past);
data_nyheter_past.add(ingress_text_past);
data_nyheter_past.add(body_text_past);
}
}
use the handler to call the same runnable obect for example when start the activity
handler.post(runnable,1000);
this will call the runnable after start the activity.
now in your runnable run method call same thing
like after 30 sec
private Runnable runnable = new Runnable(){
public void run(){
........ ///do something
handler.post(runnable,30000);
}
}
this will call continuously until you don't remove the runnable from handler object
like this way
handler.removeCallback(runnable);
You may use AlarmManager it supports pereodical execution
精彩评论