[英]Android - executing different functions with multiple threads
I am pretty new to Android programming but got some experience in other languages. 我对Android编程很陌生,但是在其他语言方面也有一些经验。 I want to create an APP which is principle working like this.
我想创建一个原理如此的APP。
The APP is a process which is asking my Web-/Database-Server every 10 seconds if there is an event to execute. APP是一个进程,每隔10秒询问我的Web /数据库服务器是否有事件要执行。
The Web-/Database-Server answers with an event id or even a function name. Web /数据库服务器用事件ID甚至函数名称回答。
My Questions are: 我的问题是:
How can I execute a function with the return value? 如何使用返回值执行函数? For example
例如
InputStream in = response.getEntity().getContent(); //Get the data in the entity
public in(void) { // execute a function which got´s the same name as the variable "in" }
The result should be: The one thread is asking every 10 seconds my Web-/Database-Server if there is an event. 结果应该是:一个线程每10秒询问我的Web- /数据库服务器是否有事件。 The event gets executed within a thread and this is working parallel (at the same time without crashing or even getting stucked).
该事件在线程内执行,并且正在并行工作(同时没有崩溃或卡住)。
Sample Code of these Threads would be appreciated. 这些线程的示例代码将不胜感激。
My code till yet: 我的代码至今:
public class service extends Service { private static final String TAG = "MyService";
公共类服务扩展了Service {private static final String TAG =“ MyService”;
@Override public IBinder onBind(Intent intent) { return null; } public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); } @Override public int onStartCommand(Intent intent, int flags, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); Thread thread = new Thread(new Runnable() { @Override public void run() { new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { System.out.println("Send GO!"); Device dev = new Device(); dev.getDeviceId(getBaseContext()); dev.getEmail(getBaseContext()); dev.sendDeviceItems(); } }, 0, 10000);//put here time 1000 milliseconds=1 second } }); thread.start(); return Service.START_STICKY; } }
Is it limited to 2 threads within the process or can I even open every time a new thread for the function which I want to execute? 是在进程中限制为2个线程,还是每次为要执行的功能创建新线程时都可以打开? Maybe because of the other function is still running?
也许因为其他功能仍在运行?
How can I execute a function with the return value? 如何使用返回值执行函数? For example
例如
InputStream in = response.getEntity().getContent(); InputStream in = response.getEntity()。getContent(); //Get the data in the entity
//获取实体中的数据
public in(void) { // execute a function which got´s the same name as the variable "in" } public in(void){//执行一个与变量“ in”同名的函数}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.