我有一个问题,我正在创建一个应用程序,我想在系统与 azan 时间匹配时在后台播放 azan 文件,无论用户正在使用应用程序的任何屏幕,azan 都应该开始播放。
我在 Azan.java 中创建了一个 asynchTask 类,但我不知道在哪里运行它,所以它总是在一分钟后运行以检查系统时间和祈祷时间,它是否应该在应用程序的欢迎屏幕或任何其他屏幕上运行。需要帮助
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;
}
最佳答案
使用处理程序类在一段时间后运行代码
代码如下:
定义处理器类的对象
private static final long GET_DATA_INTERVAL = 1000;
Handler hand = new Handler();
将此代码放入您创建 Activity 的方法中:
hand.postDelayed(run, GET_DATA_INTERVAL);
在主类中添加 void run 方法
Runnable run = new Runnable() {
@Override
public void run() {
new azan().execute();
hand.postDelayed(run, GET_DATA_INTERVAL);
};
关于Android:安排 AsyncTask 类在一分钟后运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23127674/