I have a problem , I am creating an application in which I want to play azan file in background whenever system matches with the azan time, azan should start playing whether the user is using any screen of app.
I have created an asynchTask class in Azan.java but I dont know where to run it so that it always run after one minute to check sysytem time with prayer Time, Whether it should run on welcome screen of app or any other. Need help
class azanBack extends AsyncTask<Void,Void,Void>{
Azan azan= new Azan();
Calendar now= Calendar.getInstance();
int hour=now.get(Calendar.HOUR);
int minute=now.get(Calendar.MINUTE);
int am_pm=now.get(Calendar.AM_PM);
String temp_time= hour+":"+minute+am_pm;
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
azan.getTime();
if(temp_time== azan.getPrayerTimes().get(0)){
azan.fplayer.start();
}else if(temp_time==azan.getPrayerTimes().get(2)){
azan.zplayer.start();
}else if(temp_time==azan.getPrayerTimes().get(3)){
azan.aplayer.start();
}else if(temp_time==azan.getPrayerTimes().get(5)){
azan.mplayer.start();
}else if(temp_time==azan.getPrayerTimes().get(6)){
azan.iplayer.start();
}
return null;
}
Use handler class to run code after some time of period
Here is code:
Define object of handler class
private static final long GET_DATA_INTERVAL = 1000;
Handler hand = new Handler();
put this code into your on create method of activity:
hand.postDelayed(run, GET_DATA_INTERVAL);
Add void run method into your main class
Runnable run = new Runnable() {
@Override
public void run() {
new azan().execute();
hand.postDelayed(run, GET_DATA_INTERVAL);
};