Android的服务线程使Web请求被阻塞UI [英] Android service thread making web request is blocking UI
问题描述
首先,我要解释一下目前的情况。
我在2服务2不同的线程(从USB端口的服务阅读和Web请求服务)。我开始他们的onCreate我的活动一样的:
First I will explain the current situation. I've 2 different threads in 2 services(read from usb port service and make web requests service). I'm starting them in onCreate of my activity like:
serialServiceIntent = new Intent(NDKSerialActivity.this, SerialService.class);
startService(serialServiceIntent);
webServiceIntent = new Intent(NDKSerialActivity.this, RecordWebService.class);
startService(webServiceIntent);
有什么错串行服务,但在RecordWebService当我提出我的GUI停止,直到来响应请求。
There is nothing wrong with serial service but in RecordWebService when I make a request my gui stops until response comes.
在code是这样的:
public class RecordWebService extends Service
{
public static final String SERVER_ADDRESS = "http://192.168.1.100:8080/MobilHM/rest";
private static final String TAG = RecordWebService.class.getSimpleName();
private RecordWebThread recordWebThread;
@Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
recordWebThread = new RecordWebThread(true);
recordWebThread.start();
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.i(TAG, "RecordWebService Destroyed");
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
}
和
public class RecordWebThread extends Thread
{
private static final String TAG = RecordWebThread.class.getSimpleName();
public boolean always;
public RecordWebThread(boolean always)
{
this.always = always;
}
@Override
public void run()
{
PatientRecord patientRecord = new PatientRecord();
while (always)
{
RestClient restClient = new RestClient(RecordWebService.SERVER_ADDRESS + "/hello");
try
{
restClient.execute(RequestMethod.GET);
}
catch (Exception e1)
{
Log.e(TAG, "", e1);
}
Log.i(TAG, "Server Response Code:->" + restClient.getResponseCode());
Log.i(TAG, "Server Response:->" + restClient.getResponse());
try
{
sleep(4 * 1000);
}
catch (InterruptedException e)
{
Log.e(TAG, "Web service interrupted", e);
}
}
}
}
我也一直在努力,除去睡觉的一部分,使线程与定时器和计时器任务运行一样:
Also I've tried to remove sleep part and make the thread to run with timer and timer task like:
public void sendRecord()
{
scanTask = new TimerTask()
{
public void run()
{
handler.post(new Runnable()
{
public void run()
{
RestClient restClient = new RestClient(RecordWebService.SERVER_ADDRESS + "/hello");
try
{
restClient.execute(RequestMethod.GET);
}
catch (Exception e1)
{
Log.e(TAG, "", e1);
}
Log.i(TAG, "Server Response Code:->" + restClient.getResponseCode());
Log.i(TAG, "Server Response:->" + restClient.getResponse());
}
});
}
};
t.schedule(scanTask, 1000, 4000);
}
但没有运气,我的GUI,当谈到restClient.execute挂起。
but no luck, my gui hangs when it comes to restClient.execute .
您可以找到RestClient.java @ http://www.giantflyingsaucer.com/blog/ p = 1462
You can find RestClient.java @ http://www.giantflyingsaucer.com/blog/?p=1462
我怎样才能让我的请求,不会阻止我的GUI线程?
How can I make my requests not block my gui thread?
编辑:
public void sendRecord()
{
scanTask = new TimerTask()
{
public void run()
{
RestClient restClient = new RestClient(RecordWebService.SERVER_ADDRESS + "/hello");
try
{
restClient.execute(RequestMethod.GET);
}
catch (Exception e1)
{
Log.e(TAG, "", e1);
}
Log.i(TAG, "Server Response Code:->" + restClient.getResponseCode());
Log.i(TAG, "Server Response:->" + restClient.getResponse());
}
};
t.schedule(scanTask, 1000, 4000);
}
如果没有处理,我把这个在我的活动的onCreate但仍UI挂。
Without handler, I call this in onCreate of my activity but still ui hanging.
推荐答案
或者你可以使用一个IntentService它将为您处理线程的问题。
Or you can use an IntentService which will handle the thread issues for you.
这是一个示例类:
public class MyService extends IntentService {
public MyService() {
super("MyService");
}
public MyService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent arg0) {
//Do what you want
}
}
然后你只需拨打:
Then you just call:
Intent intent = new Intent(getApplicationContext(),MyService.class);
startService(intent);
编辑:
要重复同样的事情,每4秒,你应该做这样的事情:
To repeat the same thing every 4 seconds you should do something like this:
PendingIntent serviceIntent= PendingIntent.getService(context,
0, new Intent(context, MyService.class), 0);
long firstTime = SystemClock.elapsedRealtime();
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
long intervalInSec = 4;
am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, intervalInSec*1000, serviceIntent)
这篇关于Android的服务线程使Web请求被阻塞UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!