在我的应用程序中(只要它是打开的),我想将我的数据与我的服务器同步。
我的策略如下:
//create the handler on which we will use postdelayed
Handler handler = new Handler();
//create the first runnable.
//Will this run on UI thread at this stage ? as it is being called from the handler ?
Runnable runnable1 = new Runnable()
{
public void run()
{
Thread t = new Thread(runnable2);
}
};
//create the second runnable.
//This is for sure being called from a thread, so it will not run on UI thread ? NO ?
Runnable runnable2 = new Runnable()
{
public void run()
{
//connect to internet
//make the check periodical
handler.postdelayed(runnable1, 1000);
}
};
//call the postdelayed.
handler.postdelayed(runnable1, 1000);
如果我希望处理程序在应用程序关闭后停止其可运行任务。如果我有几个活动并且当他/单击主页按钮时我不知道用户在哪里,我该怎么办。我应该检查所有 onDestroys() 吗?