[英]How to start a service without slow down the application?
In my Android Application, I'm running a service who slows a lot my application. 在我的Android应用程序中,我正在运行一项会使我的应用程序运行速度减慢的服务。 I don't know if there's a best way that the one I choose.
我不知道我选择的最佳方法是什么。 I think it's because I use the sleep() function to check every x seconds if something changes on a website but I'm absolutely not sure about this.
我认为这是因为我使用sleep()函数每隔x秒检查一次网站上是否有更改,但是我对此绝对不确定。 Here's the code:
这是代码:
package com.example.goo;
//import
public class ServiceLive extends Service{
ThreadDemo td = new ThreadDemo();
String[] tabAllTeams;
String[] tabAllScores;
String code;
Document doc;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("Lancement de mon service");
td.run();
}
public String[] getAllTeams(){
return tabAllTeams;
}
public String[] getAllScores(){
return tabAllScores;
}
private class ThreadDemo extends Thread{
@Override
public void run() {
super.run();
try{
ThreadDemo.sleep(3000*10);
System.out.println("check again");
new NetworkOperation().execute();
}catch(Exception e){
e.getMessage();
}
new NetworkOperation().execute();
}
}
//Get All Data
class NetworkOperation extends AsyncTask<Void, Void, String > {
protected String doInBackground(Void... params) {
try {
//Définir Site De Récupération
doc = Jsoup.connect("http://www.siteduzero.com").get();
//Définir Classe de Récupération
Elements getId = doc.getElementsByClass("page-visitors");
code = getId.text();
} catch (Exception e) {
e.printStackTrace();
}
td.run();
return null;
}
}
}
What's the part that slows the app ? 什么会使应用程序变慢? Thank's
谢谢
SOLUTION: 解:
Check the answer of Maurice Gavin here: How to start service in new thread in android 在此处检查Maurice Gavin的答案: 如何在android中的新线程中启动服务
You are running the Thread
on the main thread of the service. 您正在服务的主线程上运行
Thread
。 The service's main thread is the same thread as the UI thread in your activity.. 服务的主线程与您的活动中的UI线程是同一线程。
Therefor the Sleep
call is running on your UI thread..... 因此,
Sleep
调用正在您的UI线程上运行.....
Please start the Thread
with .start()
instead of .run()
请使用
.start()
而不是.run()
启动Thread
Wiht .run()
you will start the runnable inside the Thread, not the thread itself... 使用
.run()
您将在线程内部启动可运行对象,而不是线程本身...
@Override
public void onCreate() {
super.onCreate();
System.out.println("Lancement de mon service");
td.start();
}
private class ThreadDemo extends Thread{
@Override
public void run() {
super.run();
try{
ThreadDemo.sleep(3000*10);
System.out.println("check again");
doInternetStuff();
}catch(Exception e){
e.getMessage();
}
doInternetStuff();
}
}
private void doInternetStuff() {
try {
//Définir Site De Récupération
doc = Jsoup.connect("http://www.siteduzero.com").get();
//Définir Classe de Récupération
Elements getId = doc.getElementsByClass("page-visitors");
code = getId.text();
} catch (Exception e) {
e.printStackTrace();
}
new ThreadDemo().start();
}
Your problem is exactly here: 您的问题正好在这里:
public void onCreate() {
super.onCreate();
System.out.println("Lancement de mon service");
td.run();
}
A service does not run on its own thread but instead on your application main thread. 服务不是在自己的线程上运行,而是在您的应用程序主线程上运行。 Moreover run does not start a new thread but runs the code of the
Runnable
object inside the thread which is making the call. 此外,run不会启动新线程,而是在进行调用的线程内运行
Runnable
对象的代码。 To conclude you're doing all your work in the UI thread. 总结一下,您正在UI线程中完成所有工作。 First of all you should call
td.start()
and I would also put this code in onStartCommand()
and not onCreate()
. 首先,您应该调用
td.start()
,我还将这段代码放在onStartCommand()
而不是onCreate()
。 onCreate()
is called once everytime the service is created but not everytime it receives an Intent. 每次创建服务时都会调用一次
onCreate()
但并非每次接收到Intent时都会调用一次。 onStartCommand()
instead runs for every Intent
received by the Service
. onStartCommand()
将为Service
接收的每个Intent
运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.