你认为 Thead.sleep() 是服务的好方法吗?
Do you think Thead.sleep() is a good approach in Service?
我有 MyService
的服务。在我的服务中,我有一个全局变量:value
,它由函数 control_Value()
控制。用于控制varialbe值的函数value
规则如下:
The time when calling the function is called initial time. At intial time, the value is set as One
. After 3 seconds from initial time, the value is set to Zero
. The value will maintain Zero
until the function is called again.
根据上面的规则,我写了control_Value()
如下:
public void control_Value()(){
value="One";
try{
Thread.sleep(3000);
value="Zero";
}
catch{}
}
您认为 Thead.sleep(3000) 是一个好的方法吗?如果没有,请给我一个更好的解决方案。请注意,上述功能运行良好。
这是我的服务
public class MyService extends Service {
String value=null;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}
@Subscribe
public void onSMSContentReceived(OnSMSReceiverEvent event) {
control_Value();
}
}
更新:当短信到达phone时自动调用onSMSContentReceived
。
这是根据 TGMCians 的建议使用倒数计时器的解决方案
//Global variable
private CountDownTimer mCountDownTimer;
//
@Override
protected void onDestroy() {
if(mCountDownTimer!=null){
mCountDownTimer.cancel();
}
super.onDestroy();
}
public void control_Value()(){
mCountDownTimer = new CountDownTimer(3000,1000) {
@Override
public void onTick(long millisUntilFinished) {
value="One";
}
@Override
public void onFinish() {
// Your stuff
value="Zero";
}
};
mCountDownTimer.start();
}
Do you think Thead.sleep(3000) is a good approach?
从来没有。没有 UI 的应用程序主线程上的服务 运行。如果您将应用程序主线程保持一定秒数,应用程序将提示 ANR 消息。
What to do
如果你想在几秒钟后执行操作,那么你可以在你的服务中使用 CountDownTimer
,它有方法 onTick
& onFinish
其中 onTick
按常规命中间隔和 onFinish
时间到时命中。
我有 MyService
的服务。在我的服务中,我有一个全局变量:value
,它由函数 control_Value()
控制。用于控制varialbe值的函数value
规则如下:
The time when calling the function is called initial time. At intial time, the value is set as
One
. After 3 seconds from initial time, the value is set toZero
. The value will maintainZero
until the function is called again.
根据上面的规则,我写了control_Value()
如下:
public void control_Value()(){
value="One";
try{
Thread.sleep(3000);
value="Zero";
}
catch{}
}
您认为 Thead.sleep(3000) 是一个好的方法吗?如果没有,请给我一个更好的解决方案。请注意,上述功能运行良好。
这是我的服务
public class MyService extends Service {
String value=null;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
//TODO for communication return IBinder implementation
return null;
}
@Subscribe
public void onSMSContentReceived(OnSMSReceiverEvent event) {
control_Value();
}
}
更新:当短信到达phone时自动调用onSMSContentReceived
。
这是根据 TGMCians 的建议使用倒数计时器的解决方案
//Global variable
private CountDownTimer mCountDownTimer;
//
@Override
protected void onDestroy() {
if(mCountDownTimer!=null){
mCountDownTimer.cancel();
}
super.onDestroy();
}
public void control_Value()(){
mCountDownTimer = new CountDownTimer(3000,1000) {
@Override
public void onTick(long millisUntilFinished) {
value="One";
}
@Override
public void onFinish() {
// Your stuff
value="Zero";
}
};
mCountDownTimer.start();
}
Do you think Thead.sleep(3000) is a good approach?
从来没有。没有 UI 的应用程序主线程上的服务 运行。如果您将应用程序主线程保持一定秒数,应用程序将提示 ANR 消息。
What to do
如果你想在几秒钟后执行操作,那么你可以在你的服务中使用 CountDownTimer
,它有方法 onTick
& onFinish
其中 onTick
按常规命中间隔和 onFinish
时间到时命中。