我创建了一个基本上是呼叫白名单的应用程序!我还实现了一项功能,即用户可以为每个增加的号码设置以分钟为单位的时间,以便在为该特定号码分配“ n”分钟后,呼叫将被断开。一切都在服务中发生!呼叫白名单功能非常完美,只有连接到我应用程序的号码才能连接,而所有其他号码都将断开连接! 但是我面临的问题是n分钟后的断开连接。 我的逻辑代码如下;
if (calltype.equals("INCOMING") || calltype.equals("OUTGOING")) {
//Getting the alloted time in minutes from database fro the dialed number!
int userTime = Integer.parseInt(whiteListDao.getLimit(number));
//checking is number is greater that 0. if less than 0 call will disconnected instantly!
if (userTime > 0) {
final int milli = userTime * 1000;
int tickTime = 1000;
final String finalNumber = number;
handler.postDelayed(new Runnable() {
@Override
public void run() {
disconnectPhoneItelephony(context);
updateMyDB(finalNumber);
}
}, milli);
} else {
disconnectPhoneItelephony(context);
}
} else {
try {
handler.removeCallbacksAndMessages(null);
} catch (Exception e) {
}
}
但是,如果时间少于5分钟,这将起作用!但是,如果添加10分钟或更长时间,则在许多情况下将不起作用!这是为什么!!请帮我解决这个问题!
答案 0 :(得分:1)
您可以将CountDownTimer用于相同的目的,这对所有Android版本都非常适用。
private CountDownTimer countDownTimer;
if(calltype.equals("INCOMING")||calltype.equals("OUTGOING"))
{
//Getting the alloted time in minutes from database fro the dialed number!
int userTime = Integer.parseInt(whiteListDao.getLimit(number));
//checking is number is greater that 0. if less than 0 call will disconnected instantly!
if (userTime > 0) {
final int milli = userTime * 1000;
int tickTime = 1000;
countDownTimer = new CountDownTimer(milli, tickTime) {
@Override
public void onTick(long millisUntilFinished) {
// timer is running
}
@Override
public void onFinish() {
// time is over
disconnectPhoneItelephony(context);
updateMyDB(finalNumber);
}
}.start();
} else {
disconnectPhoneItelephony(context);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (countDownTimer != null) {
countDownTimer.cancel();
}
}