I/O 内部线程仍然生成 android.os.NetworkOnMainThreadException
I/O inside thread still generates android.os.NetworkOnMainThreadException
以下代码生成android.os.NetworkOnMainThreadException
我做错了什么?
Thread t = new Thread() {
public void run() {
Intent i = new Intent(EntryActivity.this, UpdateBizLocationService.class);
startService(i);
}
};
t.start();
Intentclass来源
来自Android服务官方文档:
Remember that if you do use a service, it still runs in your
application's main thread by default, so you should still create a new
thread within the service if it performs intensive or blocking
operations.
https://developer.android.com/guide/components/services.html
因此,您需要在服务内创建一个新线程,而不是在服务外。
或者使用已经使用工作线程的 IntentService:
https://developer.android.com/reference/android/app/IntentService.html
以下代码生成android.os.NetworkOnMainThreadException
我做错了什么?
Thread t = new Thread() {
public void run() {
Intent i = new Intent(EntryActivity.this, UpdateBizLocationService.class);
startService(i);
}
};
t.start();
Intentclass来源
来自Android服务官方文档:
Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.
https://developer.android.com/guide/components/services.html
因此,您需要在服务内创建一个新线程,而不是在服务外。
或者使用已经使用工作线程的 IntentService: https://developer.android.com/reference/android/app/IntentService.html