我正在尝试实现后台服务以将日志从调用日志数据库传输到服务器,并且我已经尝试了很多东西...计时器,线程无限循环...但似乎没有任何效果...以下程序可能是计时器循环...但是它们在某些手机上都失败了...两种实现都遵循相同的方法,但实现方式不同(显然)...所以我的问题是我没有检查某些条件这导致了无法发送数据的故障....我知道数据在睡眠时停止发送,但几乎所有手机都会恢复数据传输,但有些手机不会....
编程结构。
public class BootableService extends Service{
@Override
public IBinder onBind(final Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// use this to start my infinite thread here which is initialised inside this service class
}
@Override
public void onStart(final Intent intent, final int startId) {
//Use this to start a telophony listener that respond to call state and add the call from the call log to a blocking queue ....
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// Create a new PhoneStateListener
listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
System.out.println("phone IDLE");
// calls a function to add the events to a queue. that have not been sent already.
addtoQueue();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("Phone in use");
break;
case TelephonyManager.CALL_STATE_RINGING:
System.out.println("Phone Ringing");
break;
}
}
};
// Register the listener wit the telephony manager
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
addToQueue()
{
//add to the queue add the call log not already transmitted to the server.
}
class handler HandleStuff{
this handles the data coming from the thread .,,,, the ID of the record sent. straight from the Android call log database. and store it.
}
Class ThreadRunning extends thread{
run(){
while(true){
// This implements take the elements from the queue if present
transmit 1 record///
sleep(20 seconds.)
}
}
}
}
逻辑只触发一次。当手机启动或安装/打开应用程序时。如果该服务尚未打开。